Home > database >  (Linux) Extract JSON value using Grep specifically (no json parser)
(Linux) Extract JSON value using Grep specifically (no json parser)

Time:07-27

I want to extract the oid value from a JSON Response using Grep in Linux and store it in the variable $oid. The JSON is stored in the variable $Response

My Code:

oid= $Response | grep -Po '"oid": *\K"[^"]\*"'

My JSON (short version):

{
"count": 1,
"items": [{
        "oid": "xyzxyzxyzxyzxyzxyzxyz",
        "creationDate": "2019-02-05T02:21:08.662 0000"
         }]
}

Actual behavior: When I echo $oid, it is empty (e.g. Grep has not extracted any value from the JSON)

Expected behavior: $oid holds the oid extracted from the JSON (in this case xyzxyzxyzxyzxyzxyzxyz)

CodePudding user response:

Since OP clearly mentioned json parsers can't be used so answering in GNU grep here. Written and tested in GNU grep with shown samples only. Also experts always advice to use json parser so if you have any go for it.

echo "$Response" | grep -ozP '(^|\n){\n"count":\s [0-9] ,\n"items":\s \[{\n\s "oid":\s "\K[^"]*'

Output will be as follows: xyzxyzxyzxyzxyzxyzxyz

Explanation of regex: Adding detailed explanation of used above regex and its only for explanation purposes, for using please refer above GNU grep command.

(^|\n){\n    ##Matching new line OR starting here followed by { and new line.
"count":\s   ##Matching "count": followed by 1 or more spaces.
[0-9] ,\n    ##Matching 1 or more digits followed by comma followed by new line.
"items":\s   ##Matching "items": followed by 1 or more spaces.
\[{\n\s      ##matching literal [ followed by { new line and spaces.
"oid":\s "   ##Matching "oid": followed by 1 or more spaces followed by " here.
\K           ##Here is GNU grep's GREAT option \K which helps us to forget previous match.
             ##Basically match everything but forget its value so that we can get only required values.
[^"]*        ##Match everything just before next occurrence of " here.

CodePudding user response:

Try this :

oid="$(grep -Po '"oid": *"\K[^"] ' <<< "$Response")"

echo "$oid"
  • Related