I use jq for reading and filtering json on the command line.
Given the json below, I’d like to update the value of b
to be 30
. jq offers the update assignment, |=
, for this.
1
2
3
4
5
6
{
"a": {
"b": 10
},
"c": 20
}
Try out the snippet at https://jqplay.org/s/t39OXPDrll-
Doing the same with that data saved to file, say myData.json
and PowerShell and passing the new value as a jq variable would look like this:
1
2
3
4
5
6
{
"a": {
"b": 10
},
"c": 20
}
In the command line, run the following:
1
2
3
4
5
6
7
8
9
# retrieve b's value
C:\opt\jq-win64.exe '.a.b' myData.json
# set 30 to a powershell variable
$newValue = 30
# assign newValue to jq variable 'jqVarToUse' and pass it to the assigment operator
C:\opt\jq-win64.exe --arg jqVarToUse $newValue '.a.b |= $jqVarToUse' myData.json
jq gives the following output, and does not modify the original file.
1
2
3
4
5
6
7
{
"a": {
"b": "30"
},
"c": 20
}