0

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)

5
  • probably you meant [^"]+ instead of a single character Commented Jul 26, 2022 at 12:28
  • [^"]* actually, fixed it in the description Commented Jul 26, 2022 at 12:37
  • probably a shell problem, since nothing is specified, I'm assuming bash. $Response isn't a command recognized by bash. and then there's no "oid" string in the error message about "command not found", so your variable is empty. Something like oid=$(echo $Response | grep ...) Commented Jul 26, 2022 at 12:43
  • @BurnsBA Yes, bash it is. Can you help me some more here? I tried to implement your solution using oid=$(echo ...) but it did not help. Commented Jul 26, 2022 at 13:01
  • copy/paste your code into shellcheck.net Commented Jul 26, 2022 at 21:27

2 Answers 2

1

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.
Sign up to request clarification or add additional context in comments.

2 Comments

Good Day @RavinderSingh13. I just tested it and it did not work for me. The echo returns nothing. I don't understand why realy. Some Additional info: The $Response holds exactly what is returned from a curl call. If i just echo $Response, it will print a massive string containing all the JSON information (not formatted in the manner of JSON but the entire message as an ongoing string it seems). May this be the problem? Also, my script is a Bash script.
@Chris.Ae, is your variable Response is set? I mean it should have value in it. I had tested this code successfully
0

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

1 Comment

That did not work for me. The echo $oid returns nothing

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.