Example JSON file:
[
{
"host": "myhost",
"schema": "AAAAA",
"lunch": "sandwich"
},
{
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
}
]
We'd like to replace each schema that is AAAAA with BBBBB. We can do this with jq:
$ jq 'map(if .schema == "AAAAA" then .schema = "BBBBB" else . end)' file.json
[
{
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
},
{
"host": "myotherhost",
"schema": "QQQQQ",
"lunch": "pizza"
}
]
If it doesn't matter what the old schema was:
$ jq 'map(.schema = "BBBBB")' file.json
[
{
"host": "myhost",
"schema": "BBBBB",
"lunch": "sandwhich"
},
{
"host": "myotherhost",
"schema": "BBBBB",
"lunch": "pizza"
}
]
Since jq is a proper JSON parser, this would work even if the file was presented in a more compact form, for example as the single line
[{"host":"myhost","schema":"AAAAA","lunch":"sandwhich"},{"host":"myotherhost","schema":"QQQQQ","lunch":"pizza"}]
For the actual problem (in comments), which is to find the element in .rules.behavior[] that has a .name key with value mPulse and then to change that element's .options.apiKey to some other value:
jq '.rules.behaviors = [.rules.behaviors[]|select(.name == "mPulse").options.apiKey = "XXX"]' file.json
That is, rewrite the .rules.behaviour array in such a way that the element whose .name key is mPulse gets a new .options.apiKey.
-iswitch to the sed command to edit the file in place.jqinstead to perform operations on JSON files. It's a better tool, since it understands the actual underlying format, so it's able to do a better job on it than awk and sed.$currentSchemadoes not expand within strong/single quotes.