json 动态Jolt规范,用于处理存在一个嵌套数组的情况以及不存在嵌套数组的情况

ecbunoof  于 2023-02-26  发布在  其他
关注(0)|答案(1)|浏览(120)

动态Jolt规范,用于处理存在一个嵌套数组的情况以及不存在嵌套数组的情况
如果我们接收到嵌套数组,我的jolt规范可以很好地工作,但是如果他们发送一个没有嵌套数组的JSON,它就会失败。
你好,
下面是我的输入JSON 1

{
  "time": "2023-02-21T14:24:34.966Z",
  "itemId": "G20864_270",
  "unitOfMeasure": "PIECE",
  "futureAvailability": [
    {
      "availableQuantity": 200,
      "ETA": "2023-03-01T00:00:00.000Z"
    },
    {
      "availableQuantity": 201,
      "ETA": "2023-03-01T00:00:00.000Z"
    }
  ]
}

输入2

{
  "time": "2023-02-22T09:13:52.168Z",
  "itemId": "G20824_510",
  "unitOfMeasure": "PIECE"
}

正在使用的震动规格

[
  {
    "operation": "shift",
    "spec": {
      "futureAvailability": {
        "*": {
          "@(2,time)": "[&1].time",
          "@(2,itemId)": "[&1].itemId",
          "@(2,unitOfMeasure)": "[&1].unitOfMeasure",
          "ETA": "[&1].ETA",
          "availableQuantity": "[&1].availableQuantity"
        }
      }
    }
  }
]

产出

[
  {
    "time": "2023-02-21T14:24:34.966Z",
    "itemId": "G20864_270",
    "unitOfMeasure": "PIECE",
    "ETA": "2023-03-01T00:00:00.000Z",
    "availableQuantity": 200
  },
  {
    "time": "2023-02-21T14:24:34.966Z",
    "itemId": "G20864_270",
    "unitOfMeasure": "PIECE",
    "ETA": "2023-03-01T00:00:00.000Z",
    "availableQuantity": 201
  }
]
new9mtju

new9mtju1#

您可以使用添加了cardinality转换的以下规范,这适用于futureAvailability数组存在的情况,因为在这种情况下,这些属性将重复返回

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "[0].&"
      },
      "futureAvailability": {
        "*": {
          "@(2,time)": "[&1].time",
          "@(2,itemId)": "[&1].itemId",
          "@(2,unitOfMeasure)": "[&1].unitOfMeasure",
          "*": "[&1].&"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "time": "ONE",
        "itemId": "ONE",
        "unitOfMeasure": "ONE"
      }
    }
  }
]

其返回

[
  {
    "time": "2023-02-21T14:24:34.966Z",
    "itemId": "G20864_270",
    "unitOfMeasure": "PIECE"
  }
]

futureAvailability不存在时。如果您确实需要返回原始文件,例如

{
  "time": "2023-02-21T14:24:34.966Z",
  "itemId": "G20864_270",
  "unitOfMeasure": "PIECE"
}

然后去掉限定符[0].

相关问题