If you think the title of this post is off, so do I. I'm happy to reformulate but it's hard to explain.
Given this:
[
{
"A": "a",
"B": "b",
"C": "c"
},
{
"D": "d",
"E": "e"
}
]
I want this:
[
{ index: 0, key: "A", value: "a" },
{ index: 0, key: "B", value: "b" },
{ index: 0, key: "C", value: "c" },
{ index: 1, key: "D", value: "d" },
{ index: 1, key: "E", value: "e" }
]
Now whether it's a single array in the end or something else, it doesn't really matter. What I'm really looking for is to transform those elements in the input array into key-value pairs, and add the index of that element from the array. So the first object in the array is "index: 0", second is "index: 1", etc.
I can get the index from the array just fine:
jq 'to_entries | .[]' sample.json
which gives me:
{
"key": 0,
"value": {
"A": "a",
"B": "b",
"C": "c"
}
}
{
"key": 1,
"value": {
"D": "d",
"E": "e"
}
}
And I can get the key-value pairs just fine too:
jq 'to_entries | .[] | .value | to_entries' sample.json
which gives me:
[
{
"key": "A",
"value": "a"
},
{
"key": "B",
"value": "b"
},
{
"key": "C",
"value": "c"
}
]
[
{
"key": "D",
"value": "d"
},
{
"key": "E",
"value": "e"
}
]
but I can't figure out how to combine the two.