通过通用键值对组合JSON [英] Combining JSON by common key-value pairs

查看:127
本文介绍了通过通用键值对组合JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在解决一个问题,似乎无法解决这个问题.这是一些数据,所以您知道我在下面谈论什么:

I'm currently working through an issue, and can't seem to figure this one out. Here's some data so you know what I'm talking about below:

foo.json

{
    "Schedule": [
        {
            "deviceId": 123,
            "reservationId": 123456,
            "username": "jdoe"
        },
        {
            "deviceId": 456,
            "reservationId": 589114,
            "username": "jsmith"
        }
    ],
    "serverTime": 1522863125.019958
}

bar.json

[
    {
        "a": {
            "b": "10.0.0.1",
            "c": "hostname1"
        },
        "deviceId": 123
    },
    {
        "a": {
            "b": "10.0.0.2",
            "c": "hostname2"
        },
        "deviceId": 456
    }
]

foobar.json

foobar.json

{
    "Schedule": [
        {
            "deviceId": 123,
            "reservationId": 123456,
            "username": "jdoe",
            "a": {
                "b": "10.0.0.1",
                "c": "hostname1"
            }
        }
        },
        {
            "deviceId": 456,
            "reservationId": 789101,
            "username": "jsmith",
            "a": {
                "b": "10.0.0.2",
                "c": "hostname2"
            }
        }
    ],
    "serverTime": 1522863125.019958
}

我正在尝试使用jq来做到这一点,并从这篇文章中获得了一些帮助: https://github.com/stedolan/jq/issues/1090 目标是能够使用某些键作为文档之间的共同点来组合JSON.数据可以嵌套任意数量的级别.在这种情况下,foo.json仅嵌套两个级别的数据,但是需要与嵌套一级的数据组合.

I'm trying to use jq to do this, and had some help from this post: https://github.com/stedolan/jq/issues/1090 The goal is to be able to combine JSON, using some key as a common point between the documents. The data may be nested any amount of levels.. In this case foo.json has nested data only two levels deep, but needs to be combined with data nested 1 level deep.

任何和所有建议都将非常有帮助.如果需要,我也很高兴澄清并回答问题.谢谢!

Any and all suggestions would be super helpful. I'm also happy to clarify and answer questions if needed. Thank you!

推荐答案

使用foobar.jq如下:

With foobar.jq as follows:

def dict(f):
  reduce .[] as $o ({}; .[$o | f | tostring] = $o ) ;

($bar | dict(.deviceId)) as $dict
| .Schedule |= map(. + ($dict[.deviceId|tostring] ))

调用:

jq -f foobar.jq --argfile bar bar.json foo.json

产生如下所示的输出.

请注意,字典中的引用对象包含完整的对象(包括"deviceId"的键/值对),但由于在jq中定义了+的方式,因此不需要del(.deviceId).

Notice that the referents in the dictionary contain the full object (including the key/value pair for "deviceId"), but it's not necessary to del(.deviceId) because of the way + is defined in jq.

{
  "Schedule": [
    {
      "deviceId": 123,
      "reservationId": 123456,
      "username": "jdoe",
      "a": {
        "b": "10.0.0.1",
        "c": "hostname1"
      }
    },
    {
      "deviceId": 456,
      "reservationId": 589114,
      "username": "jsmith",
      "a": {
        "b": "10.0.0.2",
        "c": "hostname2"
      }
    }
  ],
  "serverTime": 1522863125.019958
}

这篇关于通过通用键值对组合JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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