Search code examples
javajsonxmltransformationjolt

unable to transform one json form to another using jolt parser


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.


Solution

  • 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].&"
            }
          }
        }
        }
    ]