All notes
Jq

Quick Start

Basic filters



# -r, --raw-output: return raw output, without start/end quotes.
# https://github.com/stedolan/jq/issues/250
gid=$(http "http://localhost:8000/me?\$select=Id" "${_headers}" | jq -r '.d.results[0].Id')
echo ${gid}

##### .[]
# Running .[] with the input [1,2,3] will produce the numbers as three separate results, rather than as a single array.
jq '.[]'
   [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
=> {"name":"JSON", "good":true}, {"name":"XML", "good":false}

jq '.[]'
   []
=>

jq '.[]'
   {"a": 1, "b": 1}
=> 1, 1

##### ,
# For instance, filter '.foo, .bar', produces both the "foo" fields and "bar" fields as separate outputs.

jq '.foo, .bar'
   {"foo": 42, "bar": "something else", "baz": true}
=> 42, "something else"

jq '.user, .projects[]'
   {"user":"stedolan", "projects": ["jq", "wikiflow"]}
=> "stedolan", "jq", "wikiflow"

jq '.[4,2]'
   ["a","b","c","d","e"]
=> "e", "c"

##### |
# Combines two filters like a pipe.

jq '.[] | .name'
   [{"name":"JSON", "good":true}, {"name":"XML", "good":false}]
=> "JSON", "XML"

Builtin functions



##### length
jq '.[] | length' [[1,2], "string", {"a":2}, null] => 2, 6, 1, 0

##### keys, keys_unsorted

jq 'keys'
   {"abc": 1, "abcd": 2, "Foo": 3}
=> ["Foo", "abc", "abcd"]

jq 'keys_unsorted'
   {"abc": 1, "abcd": 2, "Foo": 3}
=> ["abc", "abcd", "Foo"]

jq 'keys'
   [42,3,35]
=> [0,1,2]

##### flatten

jq 'flatten'
   [1, [2], [[3]]]
=> [1, 2, 3]

jq 'flatten(1)'
   [1, [2], [[3]]]
=> [1, 2, [3]]

jq 'flatten'
   [[]]
=> []

jq 'flatten'
   [{"foo": "bar"}, [{"foo": "baz"}]]
=> [{"foo": "bar"}, {"foo": "baz"}]