Chris.Ae
Chris.Ae

Reputation: 11

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

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)

Upvotes: 0

Views: 1188

Answers (2)

Philippe
Philippe

Reputation: 26592

Try this :

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

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

echo "$oid"

# output : xyzxyzxyzxyzxyzxyzxyz

Upvotes: 0

RavinderSingh13
RavinderSingh13

Reputation: 133528

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.

Upvotes: 1

Related Questions