2

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.

2
  • 1
    Cross posted: unix.stackexchange.com/q/590365 Commented Jun 2, 2020 at 5:25
  • Why are the numbers strings and not integers? Commented Jun 2, 2020 at 6:42

4 Answers 4

9

It's not a good idea to use line-oriented tools for parsing/manipulating JSON data. Use 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.

Sign up to request clarification or add additional context in comments.

4 Comments

If I don't have the tool jq processor, what other command can i use?
@Owen No idea, python maybe.
jq can be downloaded as standalone tool
For anyone else reading this: If the json data is coming from another source, you can pipe it to jq and eventually output it to a different file. Just don't forget the leading . char.
3

You need to install powerful JSON querying processor like jq processor. you can can easily install from here

once you install jq processor, try following command to extract the variable from JSON key value

value=($(jq -r '.X_serial' yourJsonFile.json))

you can modify the $value as you preferred operations

Comments

1

With pure Javascript: and :

node <<EOF
var o=$(</tmp/file);
o["LINE"]["X_serial"] = parseInt(o["LINE"]["X_serial"]) + 1;
console.log(o);
EOF

 Output

{ LINE:
   { A_serial: '1234',
     B_serial: '2345',
     C_serial: '3456',
     X_serial: 78 }
}

Comments

0

sed or perl, depending on whether you just need string substitution or something more sophisticated, like arithmetic.

Since you tried grep and awk, let's start with sed:

In all lines that contain TEXT, replace foo with bar

sed -n '/TEXT/ s/foo/bar/ p'

So in your case, something like:

sed -n '/X_serial/ s/\"76\"/\"77\"/ p'

or

$ cat 123.json | sed  '/X_serial/ s/\"76\"/\"77\"/' > new.json

This performs a literal substiution: "76" -> "77"

If you would like to perform arithmetic, like "+1" or "+10" then use perl not sed:

$ cat 123.json | perl -pe 's/\d+/$&+10/e if /X_serial/'
{
  "LINE" : {
    "A_serial" : "1234",
    "B_serial" : "2345",
    "C_serial" : "3456",
    "X_serial" : "86"
  }
}

This operates on all lines containing X_serial (whether under "LINE" or under something else), as it is not a json parser.

2 Comments

Since the value of "X_serial" will change which means i can't use exactly "76\", Any other suggestion?
@Owen, Ok, I edited it to include arithmetic. I personally prefer using shell tools (like awk, sed, perl etc...) over jq whenever the task can be done with them. But if you need to read the json to find only X_serial under LINE, then yes you have to use jq or some other json parsing tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.