Jolt将数组对象进行转换,并将一些字段移动到嵌套数组中

huangapple go评论51阅读模式
英文:

Jolt Transform an Array Objects and Move Some Fields to Nested Array

问题

I want to transform the following array of diagnoses and move all the fields except sequence to a nested coding array.

JSON Input

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

Desired Output

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

I can transform a single diagnoses object as expected but can't correctly transpose the array. Here's an example of the input and spec for transforming a single diagnoses object.

JSON Input (single object)

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

Jolt Spec

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

I want to transform the following array of diagnoses and move all the fields except sequence to a nested coding array.

JSON Input

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

Desired Output

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

I can transform a single diagnoses object as expected but can't correctly transpose the array. Here's an example of the input and spec for transforming a single diagnoses object.

JSON Input (single object)

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

Jolt Spec

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

答案1

得分: 1

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

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

Ignore that you have 2 different types of input and write your own Jolt spec. Because you have your array input in the sequence key, or otherwise you have them in the root object.

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

huangapple
  • 本文由 发表于 2023年6月9日 02:55:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434918.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定