0

I have a json file and I want to add some value from top in another place in json. I am trying to use jq command line.

{
    "channel": "mychannel",
    "videos": [
        {
            "id": "10",
            "url": "youtube.com"
        },
        {
            "id": "20", 
            "url": "youtube.com"
        }
    ]
}

The output would be:

{
    "channel": "mychannel",
    "videos": [
        {
            "channel": "mychannel",
            "id": "10",
            "url": "youtube.com"
        },
        {
            "channel": "mychannel",
            "id": "20", 
            "url": "youtube.com"
        }
    ]
}

in my json the "channel" is static, same value always. I need a way to concatenate always in each video array.

Someone can help me?

jq .videos + channel

1 Answer 1

3

Use a variable to remember .channel in the later stages of the pipeline.

$ jq '.channel as $ch | .videos[].channel = $ch' tmp.json
{
  "channel": "mychannel",
  "videos": [
    {
      "id": "10",
      "url": "youtube.com",
      "channel": "mychannel"
    },
    {
      "id": "20",
      "url": "youtube.com",
      "channel": "mychannel"
    }
  ]
}
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.