json Jolt变换数组对象并将某些字段移动到嵌套数组中

w8biq8rn  于 2023-06-25  发布在  其他
关注(0)|答案(1)|浏览(149)

我想转换下面的diagnoses数组,并将除sequence之外的所有字段移动到嵌套的coding数组中。

JSON输入

{
  "diagnoses": [
    {
      "version": "20230607",
      "code": "A100",
      "display": "Diagnosis 1",
      "sequence": 1
    },
    {
      "version": "20230607",
      "code": "B100",
      "display": "Diagnosis 2",
      "sequence": 2
    }
  ]
}

期望输出

{
  "diagnoses": [
    {
      "coding": [
        {
          "version": "20230607",
          "code": "A100",
          "display": "Diagnosis 1"
        }
      ],
      "sequence": 1
    },
    {
      "coding": [
        {
          "version": "20230607",
          "code": "B100",
          "display": "Diagnosis 2"
        }
      ],
      "sequence": 2
    }
  ]
}

我可以按照预期转换单个diagnoses对象,但不能正确地转置数组。下面是一个转换单个diagnoses对象的输入和规范示例。

JSON输入(单对象)

{
  "version": "20230607",
  "code": "A100",
  "display": "Diagnosis 1",
  "sequence": 1
}

震动规格

[
  {
    "operation": "shift",
    "spec": {
      "sequence": "&",
      "*": "coding[0].&"
    }
  }
]
qxgroojn

qxgroojn1#

忽略你有两种不同类型的输入,编写自己的Jolt规范。因为你有数组输入在sequence键中,否则你有它们在根对象中。

[
  {
    "operation": "shift",
    "spec": {
      "diagnoses": {
        "*": {
          "*": "&2[&1].coding[0].&",
          "sequence": "&2[&1].&"
        }
      },
      "*": "coding[0].&",
      "sequence": "&"
    }
  }
]

相关问题