I have a Json file which I want to update using jq. I am working on Ubuntu with jq-1.6
test_data.json
{
"A": "12",
"B": "34",
"C": [
["X", "test1"],
["Y", "test2"],
["Z", "test3"]
]
}
Now I want to update array C with new key:value pair. But, if the any of the key already exists then it's value should be updated.
update='[
["Z", "test4"],
["D", "test5"],
["E", "test6"]
]'
In this case the item Z already exists in test_data.json but update has new value for the item.
Expected output:
{
"A": "12",
"B": "34",
"C": [
["X", "test1"],
["Y", "test2"],
["Z", "test4"],
["D", "test5"],
["E", "test6"]
]
}
So far, I could do
cat test_data.json | jq --argjson val "${update}" '.C += $val')
But this is not updating value for item Z, instead adding new entry.
Can anyone please let me know how to resolve this?
Thanks in advance.
Zis not a key, it's just the first (regular) item of an array. Keys are a concept of objects like{"Z": "test4"}. (Note the curly braces)