1

我正在尝试解析一个 json 字符串值。它看起来像一个 json 嵌套对象,但它是一个键的字符串值。我可以-r在 jq 上使用来做原始输出,它去掉了双引号,从而使它成为一个 json 对象......或者我认为。也删除了-r逃逸。但是,它并没有像我期望的那样“漂亮地打印”它。我尝试了不同的解析方式,管道蒸汽等,但没有运气。

这是我的 json 小块,我将按原样和原始方式将其 jq。如您所见,我将进入rows数组并获取键值等。我正在尝试获取cpu. 下面的片段只是我尝试过的无数事情的片段。

{
 "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"
# %

但是,如果我将该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": {
    "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"
# %
4

0 回答 0