Working with JSON on CLI🔗
Format, parse, transform and extract JSON data (e.g: from an API response using curl) with jq:
jq is like sed for JSON data - you can use it to slice and filter and map and transform structured data with the same ease that sed, awk, grep and friends let you play with text.
Installation🔗
OS X🔗
1  |  | 
Debian🔗
1  |  | 
Manala🔗
1 2 3 4  |  | 
1 2 3 4 5 6  |  | 
Usage🔗
Simply use jq by piping a JSON to it:
1  |  | 
to get parsed and syntax highlighted JSON.
Of course you can go further and start working with the jq expression language to transform & extract the data you need.
Usage samples🔗
Given:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35  |  | 
Get an object property value🔗
1  |  | 
where data is a property of the root JSON object
▶️ Test it
Extract a specific collection item🔗
1  |  | 
where data is a property of the root JSON object (.), holding a collection of items (array)
▶️ Test it
Use [-1] to extract the last item.
Use [1, 2] to extract only some items.
Use [.data[1, 2]] to wrap back the items in a collection.
Use .data[1:3] to extract a range.  
Unwrap a collection🔗
1  |  | 
▶️ Test it
Flatten an object🔗
1  |  | 
casting it to an array, removing its keys
▶️ Test it
Use .data[1][] to unwrap all properties values directly (not as an array).
Filter a collection🔗
1  |  | 
get only the processed items
▶️ Test it
Use .data[] | select( .status | contains("processed") | not ) to negate the condition and only get non processed items.
Transform items🔗
1  |  | 
extract duration and id into a normalized object for each item in the collection
▶️ Test it
Use .data | map([ .duration, .updateId ]) to transform to an array instead.
Using variables🔗
Store object properties in $keys var, data length in $count and display them into a JSON object:
1  |  | 
▶️ Test it
To CSV🔗
The @csv formatter allows to convert arrays to csv strings:
1  |  | 
▶️ Test it
Note
Use the -r option to output as raw strings instead of JSON strings.
Considering this sample:
1 2 3 4 5 6 7 8 9 10 11 12 13 14  |  | 
We first need to convert each object in the collection to an array, then convert each to csv:
1  |  | 
▶️ Test it
A more advanced sample, auto-detecting keys as CSV header:
1  |  | 
Warning
We assume the properties of each rows are the same and ordered the same way.
Otherwise, use map to normalize the rows.
▶️ Test it
Complex sample:
1  |  | 
gets a CSV of id and duration of every processed items in a collection
▶️ Test it
References🔗
- ./jq official website
 - ./jq manual
 - olih/jq-cheetsheet.md
 - JQ Play: a playground for JQ