I would like to parse data from a JSON-file into a tsv-file, but I cannot make it work.
Input
{
"images": [
{
"id": "592a77a5-614e-4ed8-b846-d4db1f27edbf",
"name": "ubuntu",
"tags": [
"latest"
]
},
{
"id": "592da7a5-614e-4ed8-b846-d4db1f27edbf",
"name": "debian",
"tags": [
"latest",
"10.0"
]
}
]
}
Desired output
The desired output is a tab-separated table
ubuntu latest
debian latest
debian 10.0
What I have tried
# My data
echo '{"images":[{"id":"592a77a5-614e-4ed8-b846-d4db1f27edbf","name":"ubuntu","tags":["latest"]},{"id":"592da7a5-614e-4ed8-b846-d4db1f27edbf","name":"debian","tags":["latest","10.0"]}]}' > my.json
# My query
jq -r '.images | map({id} (.tags | fromjson[])) | @tsv' my.json > my.tsv
First I access the images-list, then I want to make an operation over all ids, where I want the tags. I borrowed the map from a similar post, but it does not seem to work. The only difference between my query and theirs is the extra layer with the .images.
CodePudding user response:
Save the name into a variable before you go down to the tags
jq -r '.images[] | .name as $name | .tags[] | [$name, .] | @tsv'
ubuntu latest
debian latest
debian 10.0