Bootstrap FreeKB - jq - Append to a JSON array
jq - Append to a JSON array

Updated:   |  jq articles

Here is an example of how you can append keys and values to a JSON list using jq.

]$ echo {} | jq '.data += [{"foo":"hello"}]' | jq '.data += [{"bar":"world"}]'
{
  "data": [
    {
      "foo": "hello"
    },
    {
      "bar": "world"
    }
  ]
}

 

Or like this.

]$ echo {\"data\":{\"greeting\":[{\"foo\":\"hello\"}]}} | jq '.data.greeting += [{"foo":"world"}]'
{
  "data": {
    "greeting": [
      {
        "foo": "hello"
      },
      {
        "foo": "world"
      }
    ]
  }
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 9de167 in the box below so that we can be sure you are a human.