I have a JSON object like this:
{
"a": {
"b": "xx",
"c": {
"d": "xx",
"e": "xx"
}
}
}
where I need to modify several nested values (for example "b" and "e"), so that the result would look like this:
{
"a": {
"b": "yy",
"c": {
"d": "xx",
"e": "yy"
}
}
}
However, if I try adding an object with the updated values I end up with everything else removed:
me@localhost:~ $ json='{"a":{"b":"xx","c":{"d":"xx","e":"xx"}}}'
me@localhost:~ $ echo "$json" | jq -r '. + {"a":{"b":"yy","c":{"e":"yy"}}}'
{
"a": {
"b": "yy",
"c": {
"e": "yy"
}
}
}
And when I modify the values sequentially I get two objects with one property changed each:
me@localhost:~ $ echo "$json" | jq -r '. | .a.b |= "yy", .a.c.e |= "yy"'
{
"a": {
"b": "yy",
"c": {
"d": "xx",
"e": "xx"
}
}
}
{
"a": {
"b": "xx",
"c": {
"d": "xx",
"e": "yy"
}
}
}