JOLT 转换具有键值对的嵌套数组

JOLT transform flatten nested array with key value pairs

我正在尝试转换以下内容JSON

{
  "data": {
    "keyvalues": [
      {
        "key": "location",
        "value": "sydney, au"
      },
      {
        "key": "weather",
        "value": "sunny"
      }
    ]
  },
  "food": {
    "name": "AllFoods",
    "date": "2018-03-08T09:35:17-03:00",
    "count": 2,
    "food": [
      {
        "name": "chocolate",
        "date": "2018-03-08T12:59:58-03:00",
        "rating": "10",
        "data": null
      },
      {
        "name": "hot dog",
        "date": "2018-03-08T09:35:17-03:00",
        "rating": "7",
        "data": {
          "keyvalues": [
            {
              "key": "topping",
              "value": "mustard"
            },
            {
              "key": "BUN type",
              "value": "toasted"
            },
            {
              "key": "servings",
              "value": "2"
            }
          ]
        }
      }
    ]
  }
}

进入,像这样更简单的东西,使用 JOLT(在 NIFI 中)。将第一个顶级 food 属性(namedatecount)放入 header,然后将嵌套的 food 数组向上拉,然后将 food.data.keyvalues 展平为 dict/hashmap。

{
  "header": {
    "location": "sydney, au",
    "weather": "sunny",
    "date": "2018-03-08",
    "count": 2
  },
  "foods": [
    {
      "name": "chocolate",
      "date": "2018-03-08T12:59:58-03:00",
      "rating": "10"
    },
    {
      "name": "hot dog",
      "date": "2018-03-08T09:35:17-03:00",
      "rating": "7",
      "topping": "mustard",
      "bun_type": "toasted",
      "servings": "2"
    }
  ]
}

我的第一个 data 部分可以正常工作,但我不确定如何处理嵌套的 food 元素。顶级 food 信息需要移动到 header 部分,第二级 food 数组需要展平 data.keyvalues.

当前规格...(仅处理顶部 data.keyvalues

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "keyvalues": {
          "*": { "@value": "@key" }
        }
      }
    }
  }
]

规格

[
  {
    "operation": "shift",
    "spec": {
      "data": {
        "keyvalues": {
          "*": {
            "value": "header.@(1,key)"
          }
        }
      },
      "food": {
        "date": "header.date",
        "count": "header.count",
        "food": {
          "*": {
            "name": "foods[&1].name",
            "date": "foods[&1].date",
            "rating": "foods[&1].rating",
            "data": {
              "keyvalues": {
                "*": {
                  "value": "foods[&4].@(1,key)"
                }
              }
            }
          }
        }
      }
    }
  }
]