无法针对具有特殊字符的特定键访问该值

时间:2018-02-20 05:51:20

标签: json jq

我试图从给定的JSON文件中提取相关数据。 JSON具有以下格式。

[
  {
    "partyResponse" : {
      "ns2:Address1" : "10 test Way",
      "ns2:Address2" : null,
      ...
    }
  },
  {
    ...
  }
  ...
]

我正在尝试获取Address1的值,但以下内容只是以奇怪的方式打印出整个JSON。

jq .[].partyResponse["ns2:Address1"] random_file_20180220.json

我正试图像这样访问它,因为密钥名称有冒号。

我只想列出以下地址。

"10 test Way"
"10 test Way"
"10 test Way"
...

1 个答案:

答案 0 :(得分:0)

找到了解决方案。

shell剥离引号。因此,将表达式放在单引号中是一种好习惯。

jq '.[].partyResponse["ns2:Address1"]' random_file_20180220.json

以上给出了理想的结果。

"10 test Way"
"10 test Way"
"10 test Way"
"10 test Way"
"10 test Way"
"10 test Way"
"3030 test Freeway"
"3030 test Freeway"

这仅适用于Linux系统(我使用的是Ubuntu)。

相关问题