insim
insim

Reputation: 21

How to add a new element using the jq command?

i have this JSON data

{
  "slotNo": "2",
  "tracking": "797aacdf9e40443aba4c818fead0195e",
  "TEST": "ASD.mpg",
  "ZXC": "555",
  "TID": "T15789",
  "AD": "123456"
}
{
  "slotNo": "3",
  "tracking": "897aacdf9e40443aba4c818fead0195e",
  "TEST": "zxc.mpg",
  "ZXC": "666",
  "TID": "T777",
  "AD": "789456"
}

And I want to append "{"List": []} to the top of the list and save the JSON data to a file. The result that I want is like this:

{
  "List": [
    {
      "slotNo": "2",
      "tracking": "797aacdf9e40443aba4c818fead0195e",
      "TEST": "ASD.mpg",
      "ZXC": "555",
      "TID": "T15789",
      "AD": "123456"
    },
    {
      "slotNo": "3",
      "tracking": "897aacdf9e40443aba4c818fead0195e",
      "TEST": "zxc.mpg",
      "ZXC": "666",
      "TID": "T777",
      "AD": "789456"
    }
  ]
}

Upvotes: 0

Views: 52

Answers (2)

glenn jackman
glenn jackman

Reputation: 247112

Using inputs to consume the rest of the input data

jq '{List: [., inputs]}'

Upvotes: 0

Barbaros Özhan
Barbaros Özhan

Reputation: 65393

You can use slurp option such as

jq -s '{ List:. }'

Demo

Upvotes: 2

Related Questions