使用 Jolt 根据公共键组合 JSON 个数组

Combine JSON arrays based on common key using Jolt

我的 JSON 对象如下所示:

{
  "array1": [
    {
      "key1": "value1", // common key
      "key2": "value2",
      "key3": "value3"
    },
    {
      "key1": "value1", // common key
      "key2": "value2",
      "key3": "value3"
    }
  ],
  "includes": {
    "array2": [
      {
        "key4": "value1", // common key
        "key5": "value5"
      },
      {
        "key4": "value1", // common key
        "key5": "value5"
      }
    ]
  }
}

我需要以下格式的输出 -

[ 
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key5": "value5"  // this comes from joining with array 2 based on key1 & key4
  }, 
  {
    "key1": "value1",
    "key2": "value2",
    "key3": "value3",
    "key5": "value5"  // this comes from joining with array 2 based on key1 & key4
  }
]

基于 ,如果两个数组中存在相同的键(例如:array1>key1 & array2>key1),我有一个答案可以使上述工作正常进行。但是,在这种情况下,两个数组都有不同的键(array1>key1 & array2>key4)当然具有相同的值。

请建议在这种情况下如何匹配键,并获得所需的输出。谢谢!

您可以使用连续的 shift 转换规范,例如

[
  { // combine key-value pairs of the arrays
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "*": "[&1].&"
          },
          "@": "[&1]"
        }
      }
    }
  },
  { // exchange key-value pairs, and take keys under common values which are pretended to be key names now 
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$": "&2.@(0)"
        }
      }
    }
  },
  { // pick the first component for the Right Hand Side if they're list of keys
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": "ONE"
      }
    }
  },
  { // get rid of key names of the objects 
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

站点 http://jolt-demo.appspot.com/ 上的 演示