JQ查询JSON文件

问题描述 投票:2回答:1

我在JSON文件中有以下代码。

{
  "comment": {
    "vm-updates": [],
    "site-ops-updates": [
      {
        "comment": {
          "message": "You can start maintenance on this resource"
        },
        "hw-name": "Machine has got missing disks. "
      }
    ]
  },
  "object_name": "4QXH862",
  "has_problems": "yes",
  "tags": ""
}

我想使用jq将“hw-name”与此JSON文件分开。我尝试了以下组合,但没有任何效果。

cat jsonfile | jq -r '.comment[].hw-name'
cat json_file.json | jq -r '.comment[].site-ops-updates[].hw-name'

来自StackOverflow的赞赏!!!

bash jq
1个回答
1
投票

它应该是:

▶ cat jsonfile | jq -r '.comment."site-ops-updates"[]."hw-name"' 
Machine has got missing disks. 

或者更好的是:

▶ jq -r '.comment."site-ops-updates"[]."hw-name"' jsonfile    
Machine has got missing disks. 

来自docs

如果键包含特殊字符,则需要用双引号将其括起来:."foo$",否则.["foo$"]

© www.soinside.com 2019 - 2024. All rights reserved.