An excerpt from the repository, fetched Nov 13, 2026.
Read the project’s introduction
Quick Start
If you use Jsawk and want to help maintain it, please let me know and I'll add you to the repo.
Updated underscore.js to v1.8.2.
Jsawk is like awk, but for JSON. You work with an array of JSON objects
read from stdin, filter them using JavaScript to produce a results array
that is printed to stdout. You can use this as a filter to manipulate data
from a REST JSON web service, for example, in a shell script. Also, you can
suppress JSON output and use the built-in printing functions to translate
your JSON input to other formats and send that to stdout, to be piped to
other processes. You can load JavaScript libraries on the command line to
increase your processing power, and other things.
Setup
This is a great blog post on setup and basic use of jsawk and resty, thanks
to @johnattebury.
You need to have the js interpreter installed. Your best bet is to navigate to
the mozilla site download and build the source
based on the maintained documentation there.
Ready? Go.
Install
First, get the jsawk script:
curl -L http://github.com/micha/jsawk/raw/master/jsawk > jsawk
Then make it executable and put it somewhere in your path:
chmod 755 jsawk && mv jsawk ~/bin/
Use
Now you can do some stuff with JSON data. Here's an example using data from
a REST service that serves JSON (we use resty
to do the HTTP requests):
resty http://example.com:8080/data*.json
GET /people/47 | jsawk 'this.favoriteColor = "blue"' | PUT /people/47
This would do a GET request on the resource /data/people/47.json, which
would result in a JSON object. Then jsawk takes the JSON via stdin and for
each JSON object it runs the little snippet of JavaScript, setting the
favoriteColor property to "blue", in this case. The modified JSON is then
output via stdout to resty again, which does the PUT request to update
the resource.
Usage
jsawk [OPTIONS] [SCRIPT]
OPTIONS
-------
-b <script> | -a <script>
Run the specified snippet of JavaScript before (-b) or after (-a)
processing JSON input. The `this` object is set to the whole JSON
array or object. This is used to preprocess (-b) or postprocess
(-a) the JSON array before or after the main script is applied.
This option can be specified multiple times to define multiple
before/after scripts, which will be applied in the order they
appeared on the command line.
-f <file>
Load and run the specified JavaScript file prior to processing
JSON. This option can be specified multiple times to load multiple
JavaScript libraries.
-h
Print short help page and exit.
-i <file>
Read input JSON from `file` instead of stdin.
-j <jsbin>
Specify path to spidermonkey js binary.
-n
Suppress printing of JSON result set.
-q <query>
Filter JSON through the specified JSONQuery query. If multiple
'-q' options are specified then each query will be performed in
turn, in the order in which they appeared on the command line.
-s <string>
Use `string` for input JSON instead of stdin.
-v <name=value>
Set global variable `name` to `value` in the script environment.
SCRIPT
------
This is a snippet of JavaScript that will be run on each element
of the input array, if input is a JSON array, or on the object if
it's an object. For each iteration, the `this` object is set to the
current element.
Using A Specific JS Binary
The path to the js binary can be specified in two different ways:
the -j command line option (see above)
the JS environment variable
Additionally, jsawk will source the following files at startup if they exist:
/etc/jsawkrc
~/.jsawkrc
These files can be used to export the JS environment variable.
Jsawk Scripting
Jsawk is intended to serve the purpose that is served by awk in the shell
environment, but instead of working with words and lines of text, it works
with JavaScript objects and arrays of objects.
In awk, a text file is split into an array of "records", each of which being
an array of "fields". The awk script that is specified on the command line is
run once for each record in the array, with the $1, $2, etc. variables
set to the various fields in the record. The awk script can set variables,
perform calculations, do various text-munging things, and print output. This
printing capablity makes awk into a filter, taking text input, transforming
it record by record, printing out the resulting modified records at the end.
Jsawk is similar, but in jsawk records are elements of the JSON input array
(if the input was a single object then there is a single record consisting
of that object). The jsawk script is run once for each record object, with
the this object set to the current record. So here the properties of the
record object are equivalent to the $1, $2, etc. in awk. The jsawk
script can then modify the record, perform calculations, do things. However,
instead of printing the modified record, the modified record is returned.
At then end, if the -n option was not specified, the resulting array is
printed as JSON to stdout.
Jsawk JavaScript Environment
Jsawk uses the Spidermonkey JavaScript interpreter, so you have access to all
of the Spidermonkey functions and whatnot. Additionally, the following
functions and properties are available from within a jsawk script:
PROPERTIES
----------
window
The global object.
IS
The input set.
RS
The result set.
_ The underscore.js object.
$_
The current record index (corresponding to the index of the
element in the IS array).
$$
The current record object (global variable corresponding to the
`this` object in the script scope).
METHODS
-------
forEach(array, string)
Compiles 'string' into a function and iterates over the 'array',
running the function once for each element. The function has
access to the special variables 'index' and 'item' which are,
Read the full README on GitHub