0

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.

1 Answer 1

1

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

Demo

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.