jq - Append to a JSON array
by
Jeremy Canfield |
Updated: January 06 2024
| 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