node - bruno-beloff/scs_analysis GitHub Wiki

docs > software repositories > scs_analysis > commands > manipulating nodes within JSON documents


DESCRIPTION

The node utility is used to extract a node or nodes from within a JSON document tree. Data is presented as a sequence of documents on stdin, and the extracted node(s) are passed to stdout. Alternatively to stdin, a single JSON document can be read from a file. The extracted node may be a leaf node or an internal node.

Using the --rename flag, a single internal or leaf node may be relocated in the document tree prior to node selection for output. The rename TO field is always output.

If the --merge flag is used, then the values of two fields A and B are joined with the JOIN string. The combined string has the location A in the document tree. If A is present and B is not present then the value is that of A alone. If B is present and A is not present, then B is discarded.

By default, only the specified nodes are passed to the output. In the --exclude mode, all nodes are passed to stdout, except the specified nodes. In the default mode, if no node path is specified, the whole input document is passed to stdout. In the --exclude mode, if no node path is specified, then nothing is output.

By default, output is in the form of a sequence of JSON documents, separated by newlines. If the array (-a) option is selected, output is in the form of a JSON array - the output opens with a '[' character, documents are separated by the ',' character, and the output is terminated by a ']' character.

Alternatively, if the node is an array or other iterable type, then it may be output as a sequence (a list of items separated by newline characters) according to the -s flag.

The ordering of output nodes is as follows:

  • Exclude mode - by input nodes
  • Default mode - by nodes specified on the command line

WARNING: node ordering is determined by the internal node structure of the input document. Thus the specified ordering: a.b.c, x.b.c, a.b.d would be rendered as: a.b.c, a.b.d, x.b.c

SYNOPSIS

node.py [-r FROM TO] [-m A B JOIN] [-x] [-a] [-s] [-f FILE] [-i INDENT] [-v] [NODE_1 .. NODE_N]

Options
--version show program's version number and exit
-h, --help show this help message and exit
-r RENAME, --rename=RENAME move the node at FROM to TO
-m MERGE, --merge=MERGE merge A and B
-x, --exclude include all nodes except the named one(s)
-a, --array output the sequence of input JSON documents as array
-s, --sequence output the contents of the input array node(s) as a sequence
-f FILENAME, --file=FILENAME read from FILE instead of stdin
-i INDENT, --indent=INDENT pretty-print the output with INDENT
-v, --verbose report narrative to stderr

EXAMPLES

csv_reader.py climate.csv | node.py -x val.bar

DOCUMENT EXAMPLE - INPUT

{"val": {"hmd": 73.5, "tmp": 10.8, "bar": ""}, "rec": "2019-02-17T08:56:53Z"}
{"val": {"hmd": 73.6, "tmp": 10.8, "bar": ""}, "rec": "2019-02-17T08:57:53Z"}

DOCUMENT EXAMPLE - OUTPUT

default mode:

{"val": {"hmd": 73.5, "tmp": 10.8}, "rec": "2019-02-17T08:56:53Z"}
{"val": {"hmd": 73.6, "tmp": 10.8}, "rec": "2019-02-17T08:57:53Z"}

array mode:

[{"val": {"hmd": 73.5, "tmp": 10.8}, "rec": "2019-02-17T08:56:53Z"}, {"val": {"hmd": 73.6, "tmp": 10.8}, "rec": "2019-02-17T08:57:53Z"}]

RESOURCES

JSON Lines