Twice as fast, 1/6th the memory

Original A json parser for the shell. A json parser for the shell. getopt ninja Now with mapping across elements Friendlier scripting. Using with other tools Creating json is now ten times easier. Twice as fast, 1/6th the memory Editable
version 8 of 8

Jshon parses, reads and creates JSON. It is designed to be as usable as possible from within the shell and replaces fragile adhoc parsers made from grep/sed/awk as well as heavyweight one-line parsers made from perl/python. Requires Jansson

Jshon loads json text from stdin, performs actions, then displays the last action on stdout. Some of the options output json, others output plain text meta information. Because Bash has very poor nested datastructures, Jshon does not try to return a native bash datastructure as a tpical library would. Instead, Jshon provides a history stack containing all the manipulations.

The big change in the latest release is switching the everything from pass-by-value to pass-by-reference. In a typical use case (processing AUR search results for 'python') by-ref is twice as fast and uses one sixth the memory. If you are editing json, by-ref also makes your life a lot easier as modifications do not need to be manually inserted through the entire stack.


Each action takes the form of a short option. Some require arguments. While many instances of jshon can be piped through each other, actions should be chained sequentially to reduce calls. All examples will work on this json sample:

jshon [actions] < sample.json

Gunnar (2013-12-13-01-24-21-951)

Please show how to create this json with jshon commands.

kk (2014-07-12-06-35-32-366)

jshon -Q -n {} -n 42 -i answer

Charles Haynes (2014-08-15-00-15-31-254)

jshon -n {} -n 1 -i "a" -n [] -s "none" -i 0 -n null -i 0 -n false -i 0 -n true -i 0 -i "b" -n {} -n 4 -i "d" -n 5 -i "e" -i "c"


(type) returns string, object, array, number, bool, null

jshon -t -> object


(length) returns an integer. Only works on string, object, array.

jshon -l -> 3


(keys) returns a newline separated list of keys. Only works on object.

jshon -k -> a b c

-e index

(extract) returns json value at "index". Only works on object, array. Negative array indexes wrap around.

jshon -e c -> {"d":4,"e":5}


(across) maps the remaining actions across the selected element. Only works on objects and arrays. Multiple -a calls can be nested, though the need is rare in practice.

jshon -e b -a -t -> bool bool null string

-s value

(string) returns a json encoded string. Can also be -inserted to an existing structure.

jshon -s "back\\slash" -> "back\\\\slash"

-n value

(nonstring/number) returns a json element to be later -inserted into an existing structure. Valid values are true, false, null, array, object, integers and floats. Abbreviates t, f, n, [] and {} respectively also work.

jshon -n object -> {}


(unstring) returns a decoded string. Works on string, int, real, boolean, null.

jshon -e b -e 3 -u -> none


(pop) pops the last manipulation from the stack, rewinding the history. Useful for extracting multiple values from one object.

jshon -e c -e d -u -p -e e -u -> 4 5

-d index

(delete) removes and item in an array or object. Negative array indexes will wrap around.

json -d b -> {"a":1,"c":{"d":4,"e":5}}

-i index

(insert) is complicated. It is the reverse of extract. Extract puts a json sub-element on the stack. Insert removes a sub-element from the stack, and inserts that bit of json into the larger array/object underneath. Use extract to dive into the json tree, delete/string/nonstring to change things, and insert to push the changes back up the tree.

Arrays are handled in a special manner. Passing integers will insert a value without overwriting. Negative integers are acceptable, as is the string 'append'. To overwrite a value in an array: delete the index, -n/s the new value, and then insert at the index.

jshon -e a -i a -> the orginal json
jshon -s one -i a -> {"a":"one", ...}


These are meta options that do not directly edit json. They are called at most once per invocation and are distinguished by capital letters.


(jsonp) strips a jsonp callback before continuing normally.


(sort) returns output sorted by key, instead of the original ordering.


(quiet) disables error reporting on stderr, so you don't have to sprinkle 2> /dev/null throughout your script.


(by-value) enables pass-by-value on the edit stack. Enable only for compatibility with old jshon scripts.


(continue) on potentially recoverable errors. For example, extracting values that don't exist will add 'null' to the edit stack instead of aborting.


Let's say you are making a script to download backgrounds from the internet. Specifically, the pictures on EarthPorn. Reddit has a json interface:


But now we need to extract the URLs out of there. I could fire up a text editor and dive into the json layout, but it is faster to explore the json using Jshon as a browser. If I find an object, I look at the keys. If an array, the first element.

$ jshon -t < earthporn.json
$ jshon -k < earthporn.json
$ jshon -e data -t < earthporn.json
$ jshon -e data -k < earthporn.json
$ jshon -e data -e children -t < earthporn.json
$ jshon -e data -e children -e 0 -t < earthporn.json
$ jshon -e data -e children -e 0 -k < earthporn.json
$ jshon -e data -e children -e 0 -e data -t < earthporn.json
$ jshon -e data -e children -e 0 -e data -k < earthporn.json
  .... SNIP
$ jshon -e data -e children -e 0 -e data -e url -u < earthporn.json

Cool, now we know where a single picture is in the hierarchy. (The -u on the end removes any json-safe encoding.) But how do we get all the pictures? Each url is under a different array index. So we use the -a action to map across all the elements of the array.

jshon -e data -e children -a -e data -e url -u < earthporn.json

Yup, simply replace the -e 0 and all the links are extracted. Of course, not all URLs are images so a quick regex cleans those up. The final script looks like

curl -s | \
jshon -e data -e children -a -e data -e url -u | \
grep '.\(jpe\|jp\|pn\)g$'

Working with *nix text processing

Jshon was made to be part of the usual text processing pipeline. However, every single -u is printed out to its own line. Most shell tools expect fields to be tab separated, and newlines between sets of fields. The paste tool does this. However, paste breaks down on blank lines so use sed to pad out the empty lines.

jshon ... | sed 's/^$/-/' |  paste -s -d "\t\t\n" | ....

The arguments need a little explaining.
-s is mysteriously needed for paste to correctly handle input.
-d is less obvious from the manpage, because it can take multiple characters which are looped through. The above example concatenates every three lines together.


Numerous! Forward slashes are not escaped, but that is a Jansson bug. Documentation is brief.

Quinn Comendant (2015-01-09-08-49-24-878)

How does jshon respond when input json is invalid?