简体   繁体   English

使用 `jq` 将键/值添加到使用另一个 json 文件作为源的 json 文件

[英]Using `jq` to add key/value to a json file using another json file as a source

Been struggling with this for a while and I'm no closer to a solution.已经为此苦苦挣扎了一段时间,但我离解决方案还很近。 I'm not very experienced using jq .我对使用jq不是很有经验。

I'd like to take the values from one json file and add them to another file when other values in the dict match.我想从一个 json 文件中获取值,并在 dict 中的其他值匹配时将它们添加到另一个文件中。 The example files below demonstrate what I'd like more clearly than an explanation.下面的示例文件比解释更清楚地展示了我想要的东西。

hosts.json:主机.json:

{
  "hosts": [
    {
      "host": "hosta.example.com",
      "hostid": "101",
      "proxy_hostid": "1"
    },
    {
      "host": "hostb.example.com",
      "hostid": "102",
      "proxy_hostid": "1"
    },
    {
      "host": "hostc.example.com",
      "hostid": "103",
      "proxy_hostid": "2"
    }
  ]
}

proxies.json:代理.json:

{
  "proxies": [
    {
      "host": "proxy1.example.com",
      "proxyid": "1"
    },
    {
      "host": "proxy2.example.com",
      "proxyid": "2"
    }
  ]
}

I also have the above file available with proxyid as the key, if this makes it easier:如果这样更容易,我还可以使用 proxyid 作为密钥的上述文件:

{
  "proxies": {
    "1": {
      "host": "proxy1.example.com",
      "proxyid": "1"
    },
    "2": {
      "host": "proxy2.example.com",
      "proxyid": "2"
    }
  }
}

Using these json files above (from the Zabbix API), I'd like to add the value of .proxies[].host (from proxies.json ) as .hosts[].proxy_host (to hosts.json ).使用上面的这些 json 文件(来自 Zabbix API),我想将.proxies[].host (来自proxies.json )的值添加为.hosts[].proxy_host (到hosts.json

This would only be when .hosts[].proxy_hostid equals .proxies[].proxyid只有当.hosts[].proxy_hostid等于.proxies[].proxyid

Desired output:所需的 output:

{
  "hosts": [
    {
      "host": "hosta.example.com",
      "hostid": "101",
      "proxy_hostid": "1",
      "proxy_host": "proxy1.example.com"
    },
    {
      "host": "hostb.example.com",
      "hostid": "102",
      "proxy_hostid": "1",
      "proxy_host": "proxy1.example.com"
    },
    {
      "host": "hostc.example.com",
      "hostid": "103",
      "proxy_hostid": "2",
      "proxy_host": "proxy2.example.com"
    }
  ]
}

I've tried many different ways of doing this, and think I need to use jq -s or jq --slurpfile , but I've reached a lot of dead-ends and can't find a solution.我已经尝试了许多不同的方法,并且认为我需要使用jq -sjq --slurpfile ,但是我已经遇到了很多死胡同并且找不到解决方案。

jq 'input as $p | map(.[].proxy_host = $p.proxies[].proxyid)' hosts.json proxies.json

I think I would need something like this as well, but not sure how to use it.我想我也需要这样的东西,但不知道如何使用它。

if.hosts[].proxy_hostid ==.proxies[].proxyid then.hosts[].proxy_host =.proxies[].host else empty end'

I've found these questions but they haven't helped:(我发现了这些问题,但它们没有帮助:(

This indeed is easier with the alternative version of your proxies.json .使用您的proxies.json的替代版本确实更容易。 All you need is to store proxies in a variable as reference, and retrieve proxy hosts from it while updating hosts.您只需将代理存储在变量中作为参考,并在更新主机时从中检索代理主机。

jq 'input as { $proxies } | .hosts[] |= . + { proxy_host: $proxies[.proxy_hostid].host }' hosts.json proxies.json

Online demo在线演示

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

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