bash 4.3+/zsh native JSON processor
- no external commands needed
- no subshells
- one file
- JSON validation and typechecking (as far as it goes in shell)
shelljq uses own array format for storing multi-dimensional objects in two-dimensional array (indexed arrays are not supported).
by default, root array is assumed to be an object, unless it contains _shjq_arr key and its value is 1, then the array is parsed as JSON array
bash array keys can represent:
- integers, null and booleans, saved as literals
[key]=1
[key]=true
[key]=null- literal string, wrapped in double quotes by default
[key]='"value"' # or 'value' if raw- JSON objects and have prefix
@Ain value (as in -A, associative) with top children keys in a list, separated with spaces
# { "key": { "key1": "bar", "key2": "baz", "key3": { "foo": "bar" } } }
[key]='@A key1 key2 key3'
[key.key1]='"bar"'
[key.key2]='"baz"'
[key.key3]='@A foo'
[key.key3.foo]='"bar"'- JSON arrays and have prefix
@ain value (as in -a, indexed) with each element index, separated with spaces
# { "key": [ "foo", "bar", "baz" ] }
[key]='@a 0 1 2'
[key.0]='"foo"'
[key.1]='"bar"'
[key.2]='"baz"'@A and @a serve as markers for JSON objects/arrays and contains pointers to its children. if you manually remove one of the keys from @A ... or @a ..., it will be ignored by the parser and not added into the JSON
by design this format is made for convenient and simple JSON projection onto associative array without enforcing types or being strict
follows similar to JQ(1)'s basic query syntax without the leading dot
- key
- key.subkey
- arr[] -> return each element individually
- arr[].key
- arr[1,2].key
- arr[-1,2].key -> -1 resolves to
arrs last index
convert $var's value into 1 or 0
convert $var's value into literal true or false
returns 0 or 1 depending on the $value (1/true = 0, 0/false = 1, * =` 1)
sanitize $string to be a valid string value
unquote $string and remove backslashes
supported types: 1/string, 2/int, 3/bool, 4/null, 5/object, 6/array
- if no
typesupplied, the value is checked with every type
trim JSON
- if
validate= true, the input is validated for structure and individual values (slower) - if
sub= true, output is only saved intojson_trim_outputinstead of being printed
build JSON object from its keys
- if
$shjq_arrvariable is set, its value is used as input array
build JSON array from key names
- if
$shjq_arrvariable is set, its value is used as input array
convert associative array into JSON
- if
sub= true, output is only saved intoarr_to_json_outputinstead of being printed
returns JSON input with highlight and indentation (pretty-print)
- if
sub= true, output is only saved intojson_pretty_outputinstead of being printed
used inside bash DEBUG trap, returns the execution time of each operation/command in μs
build a new array from a key object/array
- if
arr_nameis supplied, its value is used as output array rawis passed tojson_to_arr- if
$shjq_arrvariable is set, its value is used as input array
- if
arr_nameis supplied, it's used as array reference (default is json_to_arr_array)- if
arr_nameis not a valid array, a new one is created instead
- if
- if
raw= true, all key string values are saved without the quoting
perform JSON query if $raw = true, double quotes are removed from the output (if the output is a string) if $sub = true, $input is omitted, otherwise $input is used as JSON input and converted into array
- if
$shjq_arrvariable is set, its value is used as input array
set/append key value to JSON
if $sub = false, modified array is built back into JSON and saved as json_set_output
- if
$shjq_arrvariable is set, its value is used as input array
remove JSON key/element
if $sub = false, modified array is built back into JSON and saved as json_remove_output
- if
$shjq_arrvariable is set, its value is used as input array