In the following json file,
{
"contacts": [
{
"name": "John",
"phone": "1234"
},
{
"name": "Jane",
"phone": "5678"
}
]
}
I need to update both phone numbers based on the name and store the whole json in a new file.
I tried stuff like:
jq '.contacts[] | select(.name == "John") | .phone = "4321"' updated_contacts.json
But then I don't know how to go back to the parent node and change Jane's one, nor retrieve the whole json.
I tried to store to root node in a variable with
as
, but it keeps unchanged.
As a temporary workaround I'm just doing this:
jq '.contacts.number = "4321" | .contacts.number = "4321"' updated_contacts.json
But I should not rely on array indexes, but names, as the original json may change.
Any idea how could I do it using jq command?
Asked by txigreman
(163 rep)
Aug 24, 2021, 09:10 AM
Last activity: Feb 20, 2025, 10:56 AM
Last activity: Feb 20, 2025, 10:56 AM