Grep command to find json key value

Please explain how to use the grep command to extract specific text from the following json -

{"result":"Customer information saved successfully with Customer id :59 and version :1","status":{"responseCode":200,"result":null,"responseMessage":"Success","responseType":"info"}}

After some searches, this seems to be possible with grep with regex. But that does not work. Could you point out the error.

cat response.txt | grep -Po '(?<="Customer id ":)[0-9]+'

Assumption: response.txt contains the above json.

If yes, please explain.

+4
source share
2 answers

you missed K:

cat response.txt | grep -Po 'Customer id :\K[0-9]+'
59

screenshot, its work: enter image description here

+5
source

Try to do this:

$ jq -r '.result' json_file | grep -oP '(?<=Customer id :)\d+'
59

Check out http://stedolan.imtqy.com/jq/manual/

0
source

Source: https://habr.com/ru/post/1569142/


All Articles