I'm new to jq, but I'm confident it can help my workflow once I figure it out. I have been provided with some data that looks something like the below:
{
"parent_name1": {
"parent_count": 2000,
"fields": {
"field1_count": 2000,
"field2_count": 2000,
...
"fieldx_count": 20
}
},
"parent_name2": {
"parent_count": 1000,
"fields": {
"field1_count": 1000,
"field2_count": 1000,
...
"fieldx_count": 100
}
},
"parent_namex": {
...
}
}
There are two things I want to do:
- Select a specific parent name and have it display the name and all children values (count, and all field counts).
- Show all parent names where field counts are less than the total parent count.
I am close with the below commands, but they still aren't quite where I want them to be:
jq '."parent_name1"'
will give me "parent_count", "fields" and all of the field counts, but does not provide the parent name.
This:
jq '.[] | {parent_count: .parent_count, fields: .fields[]} | select(.fields < .parent_count)'
returns "parent_count" and a total field count, but not the parent name or the counts for the individual fields. I know the summed field count comes from {fields: .fields[]}, but I cannot find any way to get even remotely close to what I want without that.
$ jq '{"parent_name1" : getpath(["parent_name1"])}' /tmp/foo.json{ "parent_name1": { "parent_count": 2000, "fields": { "field1_count": 2000, "field2_count": 2000, "fieldx_count": 20 } } }