I need to set some new value using jq command in a JSON file.
Here is an example of file.json
{
"Balise1": true,
"Balise2": true,
"OtherThingEnabled": false,
"proxySettings": {
"port": 0
},
"mailSettings": {},
"maxRunningActivitiesPerJob": 5,
"maxRunningActivities": 5,
}
In order to set the proxySettings value I use the following command
jq --arg host "proxy.hub.gcp.url.com" --arg port "80" '.proxySettings = { host: $host, port: $port }' file.json
Instead of printing the modified version of file.json, it prints both original and modified version like that:
{
"Balise1": true,
"Balise2": true,
"OtherThingEnabled": false,
"proxySettings": {
"port": 0
},
"mailSettings": {},
"maxRunningActivitiesPerJob": 5,
"maxRunningActivities": 5,
}
{
"Balise1": true,
"Balise2": true,
"OtherThingEnabled": false,
"proxySettings": {
"host": "proxy.hub.gcp.url.com"
"port": "80"
},
"mailSettings": {},
"maxRunningActivitiesPerJob": 5,
"maxRunningActivities": 5,
}
I was expecting to print only the modified version.
How could I create a new JSON file only with the modified version?
2
Answers
I found the issue. I my shell I was modifying 2 thing on the same command like this :
The command was printing the first change, then the second one. I had to create two seperate jq command.
Comma
,
feeds the same input to two filters, therefore programs such., .
will output their input twice.Demo:
Instead, you want to combine both filters sequentially with the pipe filter
|
, since the plain assignment operator=
outputs its modified input.Your initial question didn’t include the full jq program (so it was missing a proper minimal reproducible example!), only your self-answer included the crucial details.