Skip to content

Getting Started

Thiago Souza edited this page Apr 9, 2018 · 2 revisions

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"}

Processing Script

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
  1. 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"

  2. 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

Basic Command-Line Usage

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]):

Inline Script

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"

File Script

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"
Clone this wiki locally