Issue
Let's say 123.json with below content:
{
"LINE" : {
"A_serial" : "1234",
"B_serial" : "2345",
"C_serial" : "3456",
"X_serial" : "76"
}
}
If I want to use a shell script to change the parameter of X_serial
by the original number +1 which is 77 in this example.
I have tried the below script to take out the parameter of X_serial
:
grep "X_serial" 123.json | awk {print"$3"}
which outputs 76
. But then I don't know how to make it into 77 and then put it back to the parameter of X_serial
.
Solution
It's not a good idea to use line-oriented tools for parsing/manipulating JSON data. Use jq instead, for example:
$ jq '.LINE.X_serial |= "\(tonumber + 1)"' 123.json
{
"LINE": {
"A_serial": "1234",
"B_serial": "2345",
"C_serial": "3456",
"X_serial": "77"
}
}
This simply updates .LINE.X_serial
by converting its value to a number, increasing the result by one, and converting it back to a string.
Answered By - oguz ismail Answer Checked By - Cary Denson (WPSolving Admin)