jq is useful to slice, filter, map and transform structured json data.
brew install jq
Not available as yum install on our current AMI. It should be on the latest AMI though: https://aws.amazon.com/amazon-linux-ami/2015.09-release-notes/
Installing from the source proved to be tricky.
When running jq, the following arguments may become handy:
Argument | Description |
---|---|
--version |
Output the jq version and exit with zero. |
--sort-keys |
Output the fields of each object with the keys in sorted order. |
The syntax for jq is pretty coherent:
Syntax | Description |
---|---|
, | Filters separated by a comma will produce multiple independent outputs |
? | Will ignores error if the type is unexpected |
[] | Array construction |
{} | Object construction |
+ | Concatenate or Add |
- | Difference of sets or Substract |
length | Size of selected element |
| | Pipes are used to chain commands in a similar fashion than bash |
Description | Command |
---|---|
Display all keys | jq 'keys' |
Adds + 1 to all items | jq 'map_values(.+1)' |
Delete a key | jq 'del(.foo)' |
Convert an object to array | to_entries | map([.key, .value]) |
Description | Command |
---|---|
Concatenate two fields | fieldNew=.field1+' '+.field2 |
Description | Command |
---|---|
All | jq .[] |
First | jq '.[0]' |
Range | jq '.[2:4]' |
First 3 | jq '.[:3]' |
Last 2 | jq '.[-2:]' |
Before Last | jq '.[-2]' |
Select array of int by value | jq 'map(select(. >= 2))' |
Select array of objects by value | ** jq '.[] | select(.id == "second")'** |
Select by type | ** jq '.[] | numbers' ** with type been arrays, objects, iterables, booleans, numbers, normals, finites, strings, nulls, values, scalars |
Description | Command |
---|---|
Add + 1 to all items | jq 'map(.+1)' |
Delete 2 items | jq 'del(.[1, 2])' |
Concatenate arrays | jq 'add' |
Flatten an array | jq 'flatten' |
Create a range of numbers | jq '[range(2;4)]' |
Display the type of each item | jq 'map(type)' |
Sort an array of basic type | jq 'sort' |
Sort an array of objects | jq 'sort_by(.foo)' |
Group by a key - opposite to flatten | jq 'group_by(.foo)' |
Minimun value of an array | jq 'min' .See also min, max, min_by(path_exp), max_by(path_exp) |
Remove duplicates | jq 'unique' or jq 'unique_by(.foo)' or jq 'unique_by(length)' |
Reverse an array | jq 'reverse' |
Hello,
I have a JSON code in the following format, and I'm having trouble figuring out how to use JQ to call information from the nested dictionaries within the objects within the list - if that makes sense. The list is the entire Value of the highest dictionary. Here is a sample of the structure:
{
"product" :
[
{
"A_key1" : "valueA1",
"A_key2": "valueA2",
"A_key3": {"keyA3x_nested" : "valueA3x_nested", "keyA3y_nested" : "valueA3y_nested"}
},
{
"B_key1" : "valueB1",
"B_key2" : "valueB2",
"B_key3" : {"keyB3x_nested" : "valueB3x_nested", "keyB3y_nested" : "valueB3y_nested"}
},
]
}
What I'm trying to do is select only some items from different positions within the hierarchy as follows:
{ "product": { "A_key1": "valueA1", "A_key3": {"keyA3y_nested": "valueA3y_nested"} "B_key1": "valueB1", "B_key3": {"keyB3y_nested": "valueB3y_nested"} } }
So far, I am able to do this on the command prompt:
curl https://sample_api | jq '{product : .product[0]}'
^ That returns the entire object for "A" which includes all 3 of A's keys, but how do I just pull information selectively from lower in the hierarchy?
Thank you.