I have a json file in the following format
{
"a": [
[
{
"p": "p1-value",
"d": "d1-value"
},
{
"p": "p2-value",
"d": "d2-value"
}
],
[
{
"p": "p3-value",
"d": "d3-value"
}
]
],
"z": [
[
{
"p": "p1-value",
"d": "d1-value"
}
]
]
}
and I'd like to know how to output something like this
a p1-value
a p2-value
a p3-value
z p1-value
I've tried a few different jq queries but I can't get it to work.
I know I can use 'keys[]' at the top level but that doesn't give me anything below it. I've tried ."a" | .[] | .[] | .path but that means I have to specify each top level key..
Thanks
Either use
to_entriesto create an array of key-value pairs, which you can access by using.keyand.value:Demo
Or fetch just the keys using
keys_unsorted, bind them to a variable usingas, and access the fields using the variable:Demo
In both cases I used string interpolation
"\(…)"to construct the final values, but depending on your processing pipeline, there might be better ways.