-1

In bash I got a json response from a mongo query:

...
json=`echo $con`
echo $json

The result looks something like this:

{ "_id" : "579289cc0", "externalId" : "2911", "externalTenantGUID" : "29d3928e53b", "name" :["X", "Y"] ... }{ "_id" : "5792892323", "externalId" : "291e31", "externalTenantGUID" : "29d3928e3253b", "name" :["X", "Y"] ... }{ "_id" : "57923320", "externalId" : "293211", "externalTenantGUID" : "29d3928322e53b", "name" :["X", "Y"] ... }

Here I want to parse this response $con and get only the values mapped to "_id", like 579289cc0, 5792892323, 57923320. I tried using sed and awk without success(to manny conditions), is there a simpler way without installing python?

Catalina Cenan
  • 319
  • 6
  • 14

3 Answers3

1

With jq:

$ jq '._id' infile
"579289cc0"
"5792892323"
"57923320"

Or, if you don't want the quotes around the results, use -r for "raw output":

$ jq -r '._id' infile
579289cc0
5792892323
57923320

If your JSON data is in a variable and not in a file, you can use a here string:

$ jq -r '._id' <<< "$json"
579289cc0
5792892323
57923320
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
0

Do something like that

   <?php

$con = '{  "products":[
                        {
                            "_id": "579289cc0",
                            "externalId": "2911",
                            "externalTenantGUID": "29d3928e53b"
                        }, {
                            "_id": "5792892323",
                            "externalId": "291e31",
                            "externalTenantGUID": "29d3928e3253b"
                        }, {
                            "_id": "57923320",
                            "externalId": "293211",
                            "externalTenantGUID": "29d3928322e53b"
                        }
                    ]
        }';
echo $con;
$JSON_OBG = json_decode($con, true);

// print_r($JSON_OBG);

$Results = '';
foreach($JSON_OBG['products'] as $OBG)
{
 $Results .=  $OBG['_id'].',';
}
$Results = rtrim($Results,',');
echo  $Results ;
?>
Tarek Adra
  • 500
  • 5
  • 12
  • It's showing the following error:./gettnt.sh: line 18: syntax error near unexpected token `(' ./gettnt.sh: line 18: `$JSON_OBG = json_decode($con, true);' Anyway there is no products key – Catalina Cenan Nov 28 '16 at 16:13
  • You have to see how to create a JSON object to work with ,,,, – Tarek Adra Dec 04 '16 at 22:53
0

Using perl regEx syntax, it can be done as below. Though this is a hacky way to achieve the use-case, recommend using jq to fix your broken json string and extract those identifiers. For now you can do,

perl -lne 'print "$1" if /.*\"_id\" : \"([[:alnum:]]+)\".*/' file
579289cc0
5792892323
57923320

Also, you can use grep with PCRE enabled, i.e. with -P flag for Perl style regular expression match.

grep -Po "\"_id\" :\K \"([[:alnum:]]+)\"" <<<"$json"
"579289cc0"
"5792892323"
"57923320"
Inian
  • 80,270
  • 14
  • 142
  • 161