Given a JSON file of the format:
{
"key00" : {
"key10" : {
"20170405" : {
"val0" : 10,
...
"valn" : 12
},
"20170404" : {
"val0" : 5,
...
"valn" : 43
},
...
},
"key11" : {...},
...
},
"key01" : {...},
"key02" : {...},
...
}
I want to use jq to decompose the tree into a flattened list whose format is such as below. This procedure should select one particular key in the hierarchy, the date, and for every instance of that date in the tree merge that date's values while making their key's unique based on the value's location in the tree:
[
{
"date" : "20170405",
"key00.key10.val0" : 10,
...
"key00.key10.valn" : 12
},
{
"date" : "20170404",
"key00.key10.val0" : 10,
...
"key00.key10.valn" : 12
},
...
{
"date" : "20170403",
"key0n.key1n.val0" : 10,
...
"key0n.key1n.valn" : 12
},
]
Knowing the nesting structure, assuming it is rigid, I have performed this with a set of for-loops in Perl. But if the structure changes, the program breaks. Also, for every level of hierarchy, I need a for-loop. How would you go about traversing this tree recursively using jq's language?
(I want to use jq because I am already using it to merge many files together of the format in the first code listing, so I figure I could build upon that. The merge is simple: jq -s 'reduce .[] as $x ({}, . * $x)' *.json > merged.json)