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' |
Having looked at your original answer request (tableIds: note the plural) and the solution you have accepted for yourself,
group_by
would never work. It's general purpose. I mean, technically, if you're gonna skip the key and hard code it, you don't need a map - you know the array values represent what you need.group_by is definitely a difficult function to work with as, IMO, it doesn't produce and intuitive output
JSON for how it's meant to work, along with output. I added an
id
field so we could clearly distinguish each object.jq:
Output:
You'll notice you now have 1 map with the key being the
tableId
? You should actually post to stack over flow - you're more likely to get help since people can get credit