【问题标题】:JQ: Merge several json arrays on index keyJQ:在索引键上合并几个 json 数组
【发布时间】:2017-07-31 15:13:02
【问题描述】:

我实际上正在寻找一种解决方案来合并基于几个“索引”键的几个 (3-4) 数组。

示例数组 1:

{"Fan":[{ "Last Name":Mueller,"Firstname":Martin,"Adress":Madisson Square,"City":"New York","DegreesIndex":3,"SchoolIndex2":1,}]

DegreesIndex 和 Schoolindex 指的是另外两个数组中的两个不同的 Key:

示例数组 2:

{"Degrees":[
{"DegreesIndex":3,
"Key":"12759303,
"Degrees":1.6}]}

示例数组 3:

{“学校”:[ {“学校索引”:1, “预告片”:“12759303.8, “教练”:米勒}]}

如何在 windows10 下将基于“索引”键的数组与 JQ 1.5 合并?

问候 蒂莫

【问题讨论】:

标签: json merge key jq


【解决方案1】:

我相信这可能与您正在寻找的内容接近:

# input: an object
def merge_by_index(obj; ix):
  ix as $index
  | . + (obj | map( select( ix == $index)) [0] )
  | del(ix) ;

清理您的样本输入后,假设 a1、a2 和 a3 是 三个顶级对象:

a1
| .Fan[0] |= merge_by_index(a2|.Degrees; .DegreesIndex)
| .Fan[0] |= merge_by_index(a3|.School; .SchoolIndex)

产生:

{
  "Fan": [
    {
      "Last Name": "Mueller",
      "Firstname": "Martin",
      "Adress": "Madisson Square",
      "City": "New York",
      "Key": 12759303,
      "Degrees": 1.6,
      "Teaser": 12759303.8,
      "Trainer": "Miller"
    }
  ]
}

【讨论】:

    【解决方案2】:

    以下是我的处理方法。这比peak 的方法要冗长一些。

    首先用数据定义一些数组。请注意,我冒昧地添加了另一个条目来测试当给定 Fan 没有匹配的 DegreesIndexSchoolIndex 时会发生什么。我喜欢在这里使用函数,因为您可以轻松地将它们替换为获取真实数据所需的任何逻辑。

    def array1: {
      "Fan": [
        {
          "Last Name":"Mueller", "First Name":"Martin",
          "Address": "Madisson Square", "City": "New York",
          "DegreesIndex": 3, "SchoolIndex": 1
        },
        {
          "Last Name":"Roberts", "First Name":"Bob",
          "DegreesIndex": 2, "SchoolIndex": 4
        },
        {
          "Last Name":"Skywalker", "First Name":"Luke",
          "DegreesIndex": 5, "SchoolIndex": 1
        }
      ]};
    
    def array2: {
     "Degrees": [
         { "DegreesIndex":3, "Key": "12759303", "Degrees":1.6 },
         { "DegreesIndex":2, "Key": "2",        "Degrees":2 }
      ]};
    
    def array3: {
      "School": [
        { "SchoolIndex":1, "Teaser":"12759303.8", "Trainer":"Miller" },
        { "SchoolIndex":2, "Teaser":"2",          "Trainer":"Miller" }
      ]};
    

    现在定义一些简单的查找函数,它们将返回与指定键匹配的记录。请注意,如果未找到该项目,则使用 [ ... ][0] 构造返回 null,而不是导致完全省略 Fan

    def LookupDegrees($i):
      [
          array2
        | .Degrees[]
        | select(.DegreesIndex == $i)
      ][0]
    ;
    
    def LookupSchool($i):
      [
          array3
        | .School[]
        | select(.SchoolIndex == $i)
      ][0]
    ;
    

    所有这些都使主程序变得简单:

      array1
    | .Fan[] 
    | .Degrees = LookupDegrees(.DegreesIndex)
    | .School = LookupSchool(.SchoolIndex)
    

    这是我使用 jq -n -f file.jq 运行时得到的结果

    {
      "Last Name": "Mueller",
      "First Name": "Martin",
      "Address": "Madisson Square",
      "City": "New York",
      "DegreesIndex": 3,
      "SchoolIndex": 1,
      "Degrees": {
        "DegreesIndex": 3,
        "Key": "12759303",
        "Degrees": 1.6
      },
      "School": {
        "SchoolIndex": 1,
        "Teaser": "12759303.8",
        "Trainer": "Miller"
      }
    }
    {
      "Last Name": "Roberts",
      "First Name": "Bob",
      "DegreesIndex": 2,
      "SchoolIndex": 4,
      "Degrees": {
        "DegreesIndex": 2,
        "Key": "2",
        "Degrees": 2
      },
      "School": null
    }
    {
      "Last Name": "Skywalker",
      "First Name": "Luke",
      "DegreesIndex": 5,
      "SchoolIndex": 1,
      "Degrees": null,
      "School": {
        "SchoolIndex": 1,
        "Teaser": "12759303.8",
        "Trainer": "Miller"
      }
    }
    

    如果您需要不同的嵌套或输出,这应该很容易调整。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-06-17
      • 2018-10-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多