I have a test.json file that looks like this:
[
{
"array": [
"string1",
"string2"
]
},
{
"object1": {
"subobject1": {},
"subobject2": {}
}
},
{
"object2": {}
}
]
I'm trying to append an element, string3 into array. I've tried the following commands:
$ jq '.[] | .array |= . + ["string3"]' test.json
$ jq '.[] | .array += ["string3"]' test.json
What I expect is this, string3 only being appended to array:
{
"array": [
"string1",
"string2",
"string3"
]
}
{
"object1": {
"subobject1": {},
"subobject2": {}
},
}
{
"object2": {},
}
However, I get these as the actual result, where the array array is being added to all objects:
{
"array": [
"string1",
"string2",
"string3"
]
}
{
"object1": {
"subobject1": {},
"subobject2": {}
},
"array": [
"string3"
]
}
{
"object2": {},
"array": [
"string3"
]
}
How can I append to the one array array without affecting any other objects in the file?
I'm using jq version 1.6.