I have a json file that needs to be updated on a certain condition.
Sample json
{
"Actions" : [
{
"value" : "1",
"properties" : {
"name" : "abc",
"age" : "2",
"other ": "test1"
}
},
{
"value" : "2",
"properties" : {
"name" : "def",
"age" : "3",
"other" : "test2"
}
}
]
}
I am writing a script that makes use of Jq to match a value and update, as shown below
cat sample.json | jq '.Actions[] | select (.properties.age == "3") .properties.other = "no-test"'
Output (printed to terminal)
{
"value": "1",
"properties": {
"name": "abc",
"age": "2",
"other ": "test1"
}
}
{
"value": "2",
"properties": {
"name": "def",
"age": "3",
"other": "no-test"
}
}
While this command makes the needed change, it outputs the entire json on the terminal and does not make change to the file itself.
Please advise if there is an option to have jq make changes on the file directly (similar to sed -i).
You'll want to update the action objects without changing the context. By having the pipe there, you're changing the context to each individual action. You can control that with some parentheses.
This should yield:
You can redirect the results to a file to replace the input file. It won't do in-place updates to a file as sed does.