🍿 Update JSON values conditionally with jq
Snacks (🍿) are my collection of recipes for solving problems. Often recorded (and cleaned up) from actual discussions where I'm involved in helping others with technical problems. Not all solutions are mine but I started collecting them in one place because you never know when you need one.
What can you do if you have a following JSON structure and you want to change the "treat" into "biscuit" in every object where "animal" is "dog"?
[
{
"animal": "cat",
"treat": "snacks",
},
{
"animal": "dog",
"treat": "snacks",
},
{
"animal": "squirrel",
"treat": "snacks",
},
]
You can use jq to filter, update and otherwise work with JSON files on command line.
First, we want to select all dogs:
jq 'map(select(.animal == "dog"))' animals.json
map
will run the command on every object in an array and select
will select all items that match the boolean condition.
This will print out
[
{
"animal": "dog",
"treat": "snacks"
}
]
To change the value of "treat", we can do that with the dot notation:
jq 'map(select(.animal == "dog").treat = "biscuit")' animals.json
which prints us:
[
{
"animal": "cat",
"treat": "snacks"
},
{
"animal": "dog",
"treat": "biscuit"
},
{
"animal": "squirrel",
"treat": "snacks"
}
]
and finally we can direct this output to a new file:
jq 'map(select(.animal == "dog").treat = "biscuit")' animals.json > new_animals.json
If your JSON only has one object (for example, if you are modifying multiple files as a batch), you can remove map
and it will work the same way on individual JSON objects.