-
Notifications
You must be signed in to change notification settings - Fork 1
Getting Started
jp
is a commmand-line tool
It requires node.js
to be to be installed. The minimum tested version is 6.0
, so any version above this should work.
To install it:
npm install -g json-processing
Then test it with:
$ echo '{ "hello": "world" }' | jp -l 'select()' -m json
{"hello":"world"}
Every jp
processing script is a javascript that starts with a call to the select()
function. In general, the function takes a yajs selection path (no need for the leading $
) that select objects from a JSON streaming and feed them to an instance of Rx.Observable, that is returned by the function to be further augmented with processing instructions.
The select()
function is defined as:
select(path: string = '$', input: Stream = process.stdin): Rx.Observable
- Arguments
name | description | default |
---|---|---|
path | A yajs selection path | $ |
input | A Node.js Stream | process stdin |
- Returns
Returns a Rx.Observable
- Examples
-
Pick an array of strings and upper case all the elements
Input:
{"strings": [ "a", "b", "c" ]}
Script:
select(".strings").map(s => s.toUpperCase())
Output:
"A" "B" "C"
-
Pick an array of numbers and sum all the elements
Input:
{"numbers": [ 1, 2, 3 ]}
Script:
select(".numbers").reduce((a, b) => a + b, 0)
Output:
6
The basic command-line usage of the jp
tool is simply piping data to the tool with a defined processing script (either inline or [in a script file]):
A script can be defined inline using the -l
parameter:
$ echo '{"strings": [ "a", "b", "c" ]}' | jp -l 'select(".strings").map(s => s.toUpperCase())' -m json
"A"
"B"
"C"
A script can also be defined in a js
file. The file must end with .js
and it must export the processing function:
module.exports = () => select(".strings").map(s => s.toUpperCase())
$ echo '{"strings": [ "a", "b", "c" ]}' | jp ./process.js -m json
"A"
"B"
"C"