How to extract elements from a string value in json, using jq

56 Views Asked by At

I am trying to parse out a json string value. It looks like a json nested object, but it's a string value for a key. I can use -r on jq to do raw output, which strips the double-quotes, and thereby makes it a json object.. or so I think. The -r also removes the escapes. However, It doesn't "pretty print" it, the way I would expect. I've tried different ways of parsing, piping steaming, etc., with no luck.

Here's my little blob of json, and I will jq it as-is, and raw. As you can see I'm going into the rows array and getting key values, etc. I'm trying to get the value(s) of cpu. The snippet below is only a snippet of the zillion things I've tried.

{
 "rows": [
  {
    "display_name": "sfsrocks",
    "name": "sfs",
    "is_muted": false,
    "mute_timeout": null,
    "meta": {
      "socket-hostname": "sfssocket"
      },
    "host_id": 449792622,
    "hw": "{\"cpu\":{\"cache_size\":\"15360 KB\",\"cpu_cores\":\"4\",\"cpu_logical_processors\":\"4\",\"family\":\"6\",\"mhz\":\"2397.223\",\"model\":\"58\",\"model_name\":\"Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz\",\"stepping\":\"0\",\"vendor_id\":\"GenuineIntel\"}}",
    "pythonV": "n/a",
    "processor": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",
    "socket-fqdn": "sofree.us.",
    "agent_version": "6.1.1"
  }
 ]
}
# %
# % cat sfstest.json | jq '.rows[]|.display_name,.name,.meta."socket-hostname",.hw'
"sfsrocks"
"sfs"
"sfssocket"
"{\"cpu\":{\"cache_size\":\"15360 KB\",\"cpu_cores\":\"4\",\"cpu_logical_processors\":\"4\",\"family\":\"6\",\"mhz\":\"2397.223\",\"model\":\"58\",\"model_name\":\"Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz\",\"stepping\":\"0\",\"vendor_id\":\"GenuineInt
el\"}}"
# %
# % cat sfstest.json | jq -r '.rows[]|.display_name,.name,.meta."socket-hostname",.hw'
sfsrocks
sfs
sfssocket
{"cpu":{"cache_size":"15360 KB","cpu_cores":"4","cpu_logical_processors":"4","family":"6","mhz":"2397.223","model":"58","model_name":"Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz","stepping":"0","vendor_id":"GenuineIntel"}}
# %
# % cat sfstest.json | jq -r '.rows[]|.display_name,.name,.meta."socket-hostname",.hw."cpu"'
sfsrocks
sfs
sfssocket
jq: error (at <stdin>:19): Cannot index string with string "cpu"
# % cat sfstest.json | jq -r '.rows[]|.display_name,.name,.meta."socket-hostname",.hw.cpu'
sfsrocks
sfs
sfssocket
jq: error (at <stdin>:19): Cannot index string with string "cpu"
# %

But if I throw that hw "value" line into a file, I can act on it as I would expect.. just not as part of the total jq line.

{"cpu":{"cache_size":"15360 KB","cpu_cores":"4","cpu_logical_processors":"4","family":"6","mhz":"2397.223","model":"58","model_name":"Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz","stepping":"0","vendor_id":"GenuineIntel"}}
# %
# % cat hw | jq '.'
{
  "cpu": {
    "cache_size": "15360 KB",
    "cpu_cores": "4",
    "cpu_logical_processors": "4",
    "family": "6",
    "mhz": "2397.223",
    "model": "58",
    "model_name": "Intel(R) Xeon(R) CPU E5-2620 v3 @ 2.40GHz",
    "stepping": "0",
    "vendor_id": "GenuineIntel"
  }
}
# %
# % cat hw | jq '.cpu.mhz'
"2397.223"
# % cat hw | jq '.cpu.cache_size'
"15360 KB"
# %
0

There are 0 best solutions below