unable to transform one json form to another using jolt parser

自闭症网瘾萝莉.ら 提交于 2020-06-29 03:49:15

问题


I have a data like below:

{
  "resourceType": "Immunization",
  "id": "example",
  "protocolApplied": [
    {
      "series": "2-dose",
      "authority": {
        "reference": "Organization/org1",
        "type": "Organization",
        "display": "xyz organization"
      },
      "targetDisease": [
        {
          "coding": [
            {
              "system": "http://snomed.info/sct",
              "code": "40468003"
            }
          ]
        }
      ],
      "doseNumberPositiveInt": 1,
      "seriesDosesPositiveInt": 10
    },
    {
      "series": "3-dose",
      "targetDisease": [
        {
          "coding": [
            {
              "system": "http://snomed.info/sct",
              "code": "66071002"
            }
          ]
        }
      ],
      "doseNumberString": "one",
      "seriesDosesString": "ten"
    }
  ]
}  

I need to transform it to get the below output:

[ {
  "resourceType" : "Immunization",
  "series" : "2-dose",
  "reference" : "Organization/org1",
  "type" : "Organization",
  "display" : "xyz organization",
  "targetDiseaseCodingSystem":"http://snomed.info/sct"
  "targetDiseaseCode":"40468003"
  "doseNumberPositiveInt" : 1,
  "seriesDosesPositiveInt" : 10
}, {
  "resourceType" : "Immunization",
  "series" : "3-dose",
   "targetDiseaseCodingSystem":"http://snomed.info/sct"
  "targetDiseaseCode": "66071002"
  "doseNumberString" : "one",
  "seriesDosesString" : "ten"
} ]  

Below is my spec:

[
  {
    "operation": "shift",
    "spec": {
      "protocolApplied": {
        "*": {
          "@(2,resourceType)": "[#2].resourceType",
          "authority": {
            // "reference": "reference"
            "*": "[#3].&"
          },
          "targetDisease": {
            "*": {
              "coding": {
                "*": {
                  //"*": "[#2].&"
                  "@(2,system)": "[#2].targetDiseaseCodingSystem"
                }
              }
            }
          },
          "*": "[#2].&"
        }
      }
    }
    }

]

I am getting below output after applying above spec:

[ {
  "resourceType" : "Immunization",
  "series" : "2-dose",
  "reference" : "Organization/org1",
  "type" : "Organization",
  "display" : "xyz organization",
  "doseNumberPositiveInt" : 1,
  "seriesDosesPositiveInt" : 10
}, {
  "resourceType" : "Immunization",
  "series" : "3-dose",
  "doseNumberString" : "one",
  "seriesDosesString" : "ten"
} ]

Where targetcodingsystem and targetdiseasecode is not populated. Please help me in this.


回答1:


Below is spec would help,

Use [&n], to shift the value n object above.

[
  {
    "operation": "shift",
    "spec": {
      "protocolApplied": {
        "*": {
          "@(2,resourceType)": "[&1].resourceType",
          "authority": {
            "*": "[&2].&"
          },
          "targetDisease": {
            "*": {
              "coding": {
                "*": {
                  "system": "[&5].targetDiseaseCodingSystem",
                  "code": "[&5].targetDiseaseCode"
                }
              }
            }
          },
          "*": "[&1].&"
        }
      }
    }
    }
]


来源:https://stackoverflow.com/questions/62191179/unable-to-transform-one-json-form-to-another-using-jolt-parser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!