简体   繁体   English

使用 jq 查询嵌套的 json

[英]Query nested json using jq

I have the json file about.json as below我有如下的 json 文件about.json

{
  "build": {
    "tag": "189",
    "buildtime": "2019-07-19-08-05"
  },
  "microservices": [
    {
      "appId": "builds",
      "imageName": "build-server:8",
      "volumesFrom": []
    },
    {
      "appId": "studio",
      "imageName": "tci-webserver:4074",
      "volumesFrom": [
        "tci-download-studio:xxx"
      ]
    },
    {
      "appId": "site",
      "imageName": "site:22",
      "volumesFrom": null
    }
  ]
}

I want to substitute value for volumesFrom for for example for first appId: "builds", but I am not able to fetch the key in the first place.我想替换volumesFrom的值,例如第一个appId:“builds”,但我无法首先获取密钥。 Could anyone help me with the command to do this.任何人都可以帮助我使用命令来执行此操作。

I have the value stored in volumes variable.我将值存储在卷变量中。 I want to assign this variable as a value to volumesFrom key for first appId:builds.我想将此变量作为值分配给第一个 appId:builds 的 volumesFrom 键。

I tried accessing the volumesFrom object using syntax such as我尝试使用诸如的语法访问volumesFrom对象

jq .microservices.volumesFrom about.json
jq .microservices.[0].volumesfrom

But none seems to fetch the key which I am expecting.但是似乎没有人获取我期望的密钥。

First you have to find the microservice object of interest:首先,您必须找到感兴趣的微服务对象:

.microservices[] | select(.appId == "builds")

Then with that, you could then update the volumesFrom value:然后,您可以更新volumesFrom值:

(.microservices[] | select(.appId == "builds")).volumesFrom = ...

https://jqplay.org/s/zLsEYAQrFC https://jqplay.org/s/zLsEYAQrFC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM