首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >需要使用jq解析json输出的帮助

需要使用jq解析json输出的帮助
EN

Stack Overflow用户
提问于 2021-09-24 14:29:43
回答 1查看 58关注 0票数 0

我有下面的json文件,我正在尝试使用jq从它解析一个特定的字段

代码语言:javascript
复制
{
  "value": [
    {
      "value": {
        "name_key": "cis.analytics.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.analytics.ServiceDescription",
        "state": "STARTED"
      },
      "key": "analytics"
    },
    {
      "value": {
        "name_key": "cis.applmgmt.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.applmgmt.ServiceDescription",
        "state": "STARTED"
      },
      "key": "applmgmt"
    },
    {
      "value": {
        "name_key": "cis.certificatemanagement.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [
              "GREEN"
            ],
            "default_message": "Health is GREEN",
            "id": "certificatemanagement.health.statuscode"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.certificatemanagement.ServiceDescription",
        "state": "STARTED"
      },
      "key": "certificatemanagement"
    },
    {
      "value": {
        "name_key": "cis.cis-license.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "The License Service is operational.",
            "id": "cis.license.health.ok"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.cis-license.ServiceDescription",
        "state": "STARTED"
      },
      "key": "cis-license"
    },
    {
      "value": {
        "name_key": "cis.cm.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.cm.ServiceDescription",
        "state": "STARTED"
      },
      "key": "cm"
    },
    {
      "value": {
        "name_key": "cis.content-library.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "Database server connection is GREEN.",
            "id": "com.vmware.vdcs.vsphere-cs-lib.db_health_green"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.content-library.ServiceDescription",
        "state": "STARTED"
      },
      "key": "content-library"
    },
    {
      "value": {
        "name_key": "cis.eam.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "",
            "id": "cis.eam.statusOK"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.eam.ServiceDescription",
        "state": "STARTED"
      },
      "key": "eam"
    },
    {
      "value": {
        "name_key": "cis.imagebuilder.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.imagebuilder.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "imagebuilder"
    },
    {
      "value": {
        "name_key": "cis.mbcs.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.mbcs.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "mbcs"
    },
    {
      "value": {
        "name_key": "cis.netdumper.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.netdumper.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "netdumper"
    },
    {
      "value": {
        "name_key": "cis.perfcharts.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "health.statsReoptInitalizer.green",
            "id": "health.statsReoptInitalizer.green"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.perfcharts.ServiceDescription",
        "state": "STARTED"
      },
      "key": "perfcharts"
    },
    {
      "value": {
        "name_key": "cis.pschealth.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.pschealth.ServiceDescription",
        "state": "STARTED"
      },
      "key": "pschealth"
    },
    {
      "value": {
        "name_key": "cis.rbd.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.rbd.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "rbd"
    },
    {
      "value": {
        "name_key": "cis.rhttpproxy.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.rhttpproxy.ServiceDescription",
        "state": "STARTED"
      },
      "key": "rhttpproxy"
    },
    {
      "value": {
        "name_key": "cis.sca.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.sca.ServiceDescription",
        "state": "STARTED"
      },
      "key": "sca"
    },
    {
      "value": {
        "name_key": "cis.sps.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.sps.ServiceDescription",
        "state": "STARTED"
      },
      "key": "sps"
    },
    {
      "value": {
        "name_key": "cis.statsmonitor.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "Appliance monitoring service is healthy.",
            "id": "com.vmware.applmgmt.mon.health.healthy"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.statsmonitor.ServiceDescription",
        "state": "STARTED"
      },
      "key": "statsmonitor"
    },
    {
      "value": {
        "name_key": "cis.topologysvc.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [
              "GREEN"
            ],
            "default_message": "Health is GREEN",
            "id": "topologysvc.health.statuscode"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.topologysvc.ServiceDescription",
        "state": "STARTED"
      },
      "key": "topologysvc"
    },
    {
      "value": {
        "name_key": "cis.updatemgr.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.updatemgr.ServiceDescription",
        "state": "STARTED"
      },
      "key": "updatemgr"
    },
    {
      "value": {
        "name_key": "cis.vapi-endpoint.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [
              "2021-09-24T14:56:06UTC",
              "2021-09-24T14:56:07UTC"
            ],
            "default_message": "Current vApi Endpoint health status is created between 2021-09-24T14:56:06UTC and 2021-09-24T14:56:07UTC.",
            "id": "com.vmware.vapi.endpoint.healthStatusProducedTimes"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.vapi-endpoint.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vapi-endpoint"
    },
    {
      "value": {
        "name_key": "cis.vcha.ServiceName",
        "startup_type": "DISABLED",
        "description_key": "cis.vcha.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "vcha"
    },
    {
      "value": {
        "name_key": "cis.vmcam.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.vmcam.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "vmcam"
    },
    {
      "value": {
        "name_key": "cis.vmonapi.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.vmonapi.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vmonapi"
    },
    {
      "value": {
        "name_key": "cis.vmware-postgres-archiver.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "VMware Archiver service is healthy.",
            "id": "cis.vmware-postgres-archiver.health.healthy"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.vmware-postgres-archiver.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vmware-postgres-archiver"
    },
    {
      "value": {
        "name_key": "cis.vmware-vpostgres.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "VMware Postgres service is healthy.",
            "id": "cis.vmware-vpostgres.health.healthy"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.vmware-vpostgres.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vmware-vpostgres"
    },
    {
      "value": {
        "name_key": "cis.vpxd.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [
              "vCenter Server",
              "GREEN"
            ],
            "default_message": "{0} health is {1}",
            "id": "vc.health.statuscode"
          },
          {
            "args": [
              "VirtualCenter Database",
              "GREEN"
            ],
            "default_message": "{0} health is {1}",
            "id": "vc.health.statuscode"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.vpxd.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vpxd"
    },
    {
      "value": {
        "name_key": "cis.vpxd-svcs.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [
          {
            "args": [],
            "default_message": "Tagging service is in a healthy state",
            "id": "cis.tagging.health.status"
          }
        ],
        "health": "HEALTHY",
        "description_key": "cis.vpxd-svcs.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vpxd-svcs"
    },
    {
      "value": {
        "name_key": "cis.vsan-dps.ServiceName",
        "startup_type": "MANUAL",
        "description_key": "cis.vsan-dps.ServiceDescription",
        "state": "STOPPED"
      },
      "key": "vsan-dps"
    },
    {
      "value": {
        "name_key": "cis.vsan-health.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.vsan-health.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vsan-health"
    },
    {
      "value": {
        "name_key": "cis.vsm.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.vsm.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vsm"
    },
    {
      "value": {
        "name_key": "cis.vsphere-client.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.vsphere-client.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vsphere-client"
    },
    {
      "value": {
        "name_key": "cis.vsphere-ui.ServiceName",
        "startup_type": "AUTOMATIC",
        "health_messages": [],
        "health": "HEALTHY",
        "description_key": "cis.vsphere-ui.ServiceDescription",
        "state": "STARTED"
      },
      "key": "vsphere-ui"
    }
  ]
}

我尝试了不同的组合,我觉得应该有效,但不能得到它

jq '.[].value.health' vm_services.json

jq: error (at vm_services.json:432):无法索引字符串为"value“的数组

jq '.[] | .value.health' vm_services.json

jq: error (at vm_services.json:432):无法索引字符串为"value“的数组

EN

回答 1

Stack Overflow用户

发布于 2021-09-24 15:07:03

您的JSON开头为:

代码语言:javascript
复制
{
  "value": [
    {

这表明您正在处理一个对象。

您的命令'.[].value.health'尝试遍历一个数组。

为此,您需要选择第一个value密钥,然后将其用作数组。

代码语言:javascript
复制
jq '.value[].value.health' tst.input

将产生:

代码语言:javascript
复制
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
null
null
null
"HEALTHY"
"HEALTHY"
null
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
null
null
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
null
"HEALTHY"
"HEALTHY"
"HEALTHY"
"HEALTHY"
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69316726

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档