Search code examples
arraysjsonapache-nifijolt

how to create specific array in jolt


im in learning process to learn jolt, but quite hard to master as there is array and the output must be the exactly the same as example below.

how to create a jolt spec form , the json input is like this :

[
  {
    "encounter_date": "1616509603296",
    "id_no": "671223025051",
    "patient_id": "MAEPS-PID-2100003716",
    "patient_mrn": "MAEPS-MRN-2100003815",
    "first_name": "MOHD RAZALI "
  },
  {
    "encounter_date": "1621324591194",
    "id_no": "950224145647",
    "patient_id": "MAEPS-PID-2100030302",
    "patient_mrn": "MAEPS-MRN-2100030401",
    "first_name": "MUHAMMAD FADDIL BIN YASIN"
  }
]

expected output is like this :

{
  "forms": [
    {
      "visit": {
        "patientId": "MAEPS-PID-2100003716",
        "Patientmrn": "MAEPS-MRN-2100003815",
        "encounterDate": "2021-03-23 22:26:43.296"
      },
      "person": {
        "firstname": "MOHD RAZALI ",
        "identifications": [
          {
            "idNo": "671223025051"
          }
        ]
      }
    },
    {
      "visit": {
        "patientId": "MAEPS-PID-2100030302",
        "Patientmrn": "MAEPS-MRN-2100030401",
        "encounterDate": "2021-05-18 15:56:31.194"
      },
      "person": {
        "firstname": "MUHAMMAD FADDIL BIN YASIN",
        "identifications": [
          {
            "idNo": "950224145647"
          }
        ]
      }
    }
  ]
}

i'm new to jolt and require guidance


Solution

  • This can be done with just a single shift operation as below.

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "patient_id": "forms[&1].visit.patientId",
            "patient_mrn": "forms[&1].visit.Patientmrn",
            "encounter_date": "forms[&1].visit.encounterDate",
            "first_name": "forms[&1].person.firstname",
            "id_no": "forms[&1].person.identifications[0].idNo"
          }
        }
      }
    ]