如何复制一个数组以附加到另一个数组?

时间:2019-08-15 06:32:24

标签: arrays jolt

我需要构造数组并将其附加到现有数组中。

我的输入json是:

{
  "restaurantId": "ZZ4ORJDY3E",
  "chainId": "RLR932KI",
  "orderItems": [
    {
      "itemName": "Small Barqs",
      "quantity": 2
    },
    {
      "itemName": "Mozzz",
      "quantity": 1
    }
  ]
}

而且,我试图将输出显示为:

{
  "array" : [ {
    "field1" : "RLR932KI",
    "field2" : "ZZ4ORJDY3E"
  }, {
    "field1" : "Small Barqs",
    "field2" : 2
  }, {
    "field1" : "Mozzz",
    "field2" : 1
  } ]
}

我现在拥有的规格:

[
  {
    "operation": "shift",
    "spec": {
      "chainId": "array[0].field1",
      "restaurantId": "array[0].field2",
      "orderItems": {
        "*": {
          "itemName": "array[&1].field1",
          "quantity": "array[&1].field2"
        }
      }
    }
  }
]

以上规格给我:

{
  "array" : [ {
    "field1" : [ "RLR932KI", "Small Barqs" ],
    "field2" : [ "ZZ4ORJDY3E", 2 ]
  }, {
    "field1" : "Mozzz",
    "field2" : 1
  } ]
}

1 个答案:

答案 0 :(得分:0)

这可以通过两阶段转换来实现:

  1. 创建一个数组数组
  2. 然后将数组的数组展平。
[
  {
    "operation": "shift",
    "spec": {
      //create array arrays
      "chainId": "[0].[0].field1",
      "restaurantId": "[0].[0].field2",
      "orderItems": {
        "*": {
          "itemName": "[1].[&1].field1",
          "quantity": "[1].[&1].field2"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      //flatten array of arrays
      "*": {
        "*": "array.[]"
      }
    }
  }
]