Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the value of key from JSON

Tags:

json

grep

I'd like to extract the "id" key from this single line of JSON.

I believe this can be accomplished with grep, but I am not sure on the correct way.

If there is a better way that does not have dependencies, I would be interested.

Here is my example output:

{
  "data": {
    "name": "test",
    "id": "4dCYd4W9i6gHQHvd",
    "domains": ["www.test.domain.com", "test.domain.com"],
    "serverid": "bbBdbbHF8PajW221",
    "ssl": null,
    "runtime": "php5.6",
    "sysuserid": "4gm4K3lUerbSPfxz",
    "datecreated": 1474597357
  },
  "actionid": "WXVAAHQDCSILMYTV"
}

like image 733
petebocken Avatar asked Sep 23 '16 03:09

petebocken


People also ask

What is key-value in JSON?

A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).

How get values from JSONArray?

JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray. length();i++){ JSONObject json_obj = jArray. getJSONObject(i); name = json_obj. getString("txt_title"); } txt_title.

Can JSON object key be a number?

JSON only allows key names to be strings. Those strings can consist of numerical values.

How do you write a key-value pair in JSON?

Each key-value pair is separated by a comma, so the middle of a JSON lists as follows: "key" : "value", "key" : "value", "key": "value" . In the previous example, the first key-value pair is "first_name" : "Sammy" . JSON keys are on the left side of the colon.


2 Answers

If you have a grep that can do Perl compatible regular expressions (PCRE):

$ grep -Po '"id": *\K"[^"]*"' infile.json
"4dCYd4W9i6gHQHvd"
  • -P enables PCRE
  • -o retains nothing but the match
  • "id": * matches "id" and an arbitrary amount of spaces
  • \K throws away everything to its left ("variable size positive look-behind")
  • "[^"]*" matches two quotes and all the non-quotes between them

If your grep can't do that, you an use

$ grep -o '"id": *"[^"]*"' infile.json | grep -o '"[^"]*"$'
"4dCYd4W9i6gHQHvd"

This uses grep twice. The result of the first command is "id": "4dCYd4W9i6gHQHvd"; the second command removes everything but a pair of quotes and the non-quotes between them, anchored at the end of the string ($).

But, as pointed out, you shouldn't use grep for this, but a tool that can parse JSON – for example jq:

$ jq '.data.id' infile.json
"4dCYd4W9i6gHQHvd"

This is just a simple filter for the id key in the data object. To get rid of the double quotes, you can use the -r ("raw output") option:

$ jq -r '.data.id' infile.json
4dCYd4W9i6gHQHvd

jq can also neatly pretty print your JSON:

$ jq . infile.json
{
  "data": {
    "name": "test",
    "id": "4dCYd4W9i6gHQHvd",
    "domains": [
      "www.test.domain.com",
      "test.domain.com"
    ],
    "serverid": "bbBdbbHF8PajW221",
    "ssl": null,
    "runtime": "php5.6",
    "sysuserid": "4gm4K3lUerbSPfxz",
    "datecreated": 1474597357
  },
  "actionid": "WXVAAHQDCSILMYTV"
}
like image 84
Benjamin W. Avatar answered Oct 06 '22 07:10

Benjamin W.


Just pipe your data to jq and select by keys

"data": {
    "name": "test",
    "id": "4dCYd4W9i6gHQHvd",
    "domains": [
      "www.test.domain.com",
      "test.domain.com"
    ],
    "serverid": "bbBdbbHF8PajW221",
    "ssl": null,
    "runtime": "php5.6",
    "sysuserid": "4gm4K3lUerbSPfxz",
    "datecreated": 1474597357
  },
  "actionid": "WXVAAHQDCSILMYTV"
} | jq '.data.id'     

# 4dCYd4W9i6gHQHvd

Tutorial Here

like image 38
jasonleonhard Avatar answered Oct 06 '22 07:10

jasonleonhard