Jolt 将键值添加到 children

Jolt add key value to children

我正在使用 NiFi Jolt 处理器来转换一些 JSON 数据。

我需要创建 object 数组。我可以从资产中获取所有 objects,但我想将 parent 中的每个日期添加到我的 object.

我有以下输入 JSON:

[
  {
    "date": "2022/01/09",
    "assets": [
      {
        "value": 1,
        "percentage": 0.1
      },
      {
        "value": 2,
        "percentage": 0.2
      }
    ],
    "liablities": []
  },
  {
    "date": "2022/01/08",
    "assets": [
      {
        "value": 3,
        "percentage": 0.3
      },
      {
        "value": 4,
        "percentage": 0.4
      }
    ],
    "liablities": []
  }
]

这是我的预期输出:

[
  {
    "value" : 1,
    "percentage" : 0.1,
    "date" : "2022/01/09"
  },
  {
    "value" : 2,
    "percentage" : 0.2,
    "date" : "2022/01/09"
  },
  {
    "value" : 3,
    "percentage" : 0.3,
    "date" : "2022/01/08"
 },
 {
   "value" : 4,
   "percentage" : 0.4,
   "date" : "2022/01/08"
  }
]

您可以使用此规范:

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "assets": {
          "*": "[].@(2,date)"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "[&2]",
          "$": "[&2].date"
        }
      }
    }
  }
]

另一种方法是应用两个连续的 shift 转换以遍历 assets 数组的索引,并修剪生成的键 01第二步内如

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "assets": {
          "*": {
            "*": "&3[&1].&",
            "@(2,date)": "&3[&1].date"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": ""
      }
    }
  }
]

Demo 已于 https://jolt-demo.appspot.com/ 准备: