Home > Enterprise >  How to add a new element using the jq command?
How to add a new element using the jq command?

Time:09-14

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"
    }
  ]
}

CodePudding user response:

You can use slurp option such as

jq -s '{ List:. }'

Demo

CodePudding user response:

Using inputs to consume the rest of the input data

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