如何使用jq合并两个JSON对象? [英] how to merge two JSON objects using jq?

查看:232
本文介绍了如何使用jq合并两个JSON对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文件json.我想将SomeFile2.json的两个数组追加到SomeFile1.json,如下所示.

I have two file json. I want to append two array of SomeFile2.json to SomeFile1.json as below.

SomeFile1.json

[
  {
    "DNSName": "CLB-test-112a877451.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0886ed703de64028a"
      }
    ]
  },
  {
    "DNSName": "CLB-test1-156925981.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0561634c4g3b4fa25"
      }
    ]
  }
]

SomeFile2.json

[
  {
    "InstanceId": "i-0886ed703de64028a",
    "State": "InService"
  },
  {
    "InstanceId": "i-0561634c4g3b4fa25",
    "State": "InService"
  }
]

我想要的结果如下:

[
  {
    "DNSName": "CLB-test-112a877451.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0886ed703de64028a"
        "State": "InService"
      }
    ]
  },
  {
    "DNSName": "CLB-test1-156925981.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0561634c4g3b4fa25"
        "State": "InService"
      }
    ]
  }
]

我正在通过jq在bash shell中进行处理.但是,不成功.

I'm processing in bash shell via jq. But, unsuccessful.

推荐答案

因为我发现很难,我以一种程序性的方式开始:使用ruby的json模块:

Since I find jq pretty hard, I started in a procedural way: using ruby's json module:

ruby -rjson -e '
  states = JSON.parse(File.read(ARGV.shift)).map {|o| [o["InstanceId"], o["State"]]}.to_h
  data = JSON.parse(File.read(ARGV.shift))
  data.each do |obj|
    obj["Instances"].each do |instance|
      instance["State"] = states[instance["InstanceId"]] || "unknown"
    end
  end
  puts JSON.pretty_generate data
' SomeFile2.json SomeFile1.json 

但是我们需要jq,所以经过一番尝试和错误之后,可以在手册中找到它: https://stedolan.github.io/jq/manual/#Complexassignments -(请注意,我更改了其中一个实例的状态,以便可以更好地验证输出)

But we want jq, so after some trial and error, and finding this in the manual: https://stedolan.github.io/jq/manual/#Complexassignments -- (note, I changed the state for one of the instances so I could verify the output better)

$ cat SomeFile2.json 
[
  {
    "InstanceId": "i-0886ed703de64028a",
    "State": "InService"
  },
  {
    "InstanceId": "i-0561634c4g3b4fa25",
    "State": "NOTInService"
  }
]

首先,将状态提取到将ID映射到状态的对象中

First, extract the states into an object mapping the id to the state:

$ state_map=$( jq -c 'map({"key":.InstanceId, "value":.State}) | from_entries' SomeFile2.json )
$ echo "$state_map"
{"i-0886ed703de64028a":"InService","i-0561634c4g3b4fa25":"NOTInService"}

然后,更新第一个文件中的实例:

Then, update the instances in the first file:

jq --argjson states "$state_map" '.[].Instances[] |= . + {"State": ($states[.InstanceId] // "unknown")}' SomeFile1.json

[
  {
    "DNSName": "CLB-test-112a877451.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0886ed703de64028a",
        "State": "InService"
      }
    ]
  },
  {
    "DNSName": "CLB-test1-156925981.ap-northeast-1.elb.amazonaws.com",
    "Instances": [
      {
        "InstanceId": "i-0561634c4g3b4fa25",
        "State": "NOTInService"
      }
    ]
  }
]

这篇关于如何使用jq合并两个JSON对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆