Search code examples
jsonapache-nifijolt

How to transform a JSON using JOLT format


Input JSON :

{
  "mtime": "2023-03-14T01:00:00Z",
  "sid": "46",
  "tsa": "9.0",
  "tha": "7.8",
  "hra": "85.0",
  "tsf": null,
  "tss": null,
  "ens": "0.0",
  "dvt": null,
  "vvt": "4.8",
  "plu": "0.1",
  "hct": "26.0"
}

Desired Output JSON :

{
  "Date": "2023-03-14 01:00:00",
  "data": [
    {
      "code": "tsa",
      "value": "9.0"
    },
    {
      "code": "tha",
      "value": "7.8"
    },
    {
      "code": "hra",
      "value": "85.0"
    },
    {
      "code": "tsf",
      "value": null
    },
    {
      "code": "tss",
      "value": null
    },
    {
      "code": "ens",
      "value": "0.0"
    },
    {
      "code": "dvt",
      "value": null
    },
    {
      "code": "vvt",
      "value": "4.8"
    },
    {
      "code": "plu",
      "value": "0.1"
    },
    {
      "code": "hct",
      "value": "26.0"
    }
  ],
  "stationname": "Station"
}

Solution

  • You can use the following transformation

    [
      {
        "operation": "shift",
        "spec": {
          "mtime": "Date", // rename the attribute
          "*": "data.&"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "Date": "&",
          "data": {
            "*": { // loop through all the attributes within the "data" array
              "$": "data[#2].code",
              "@": "data[#2].value"
            }
          },
          "#Station": "stationname" // add an attribute with fixed value
        }
      },
      { // just to sort the elements in the deisred order
        "operation": "shift",
        "spec": {
          "Date|data": "&",
          "stationname": "&"
        }
      },
      { // 
        "operation": "modify-overwrite-beta",
        "spec": {
          "DPortion1": "=substring(@(1,Date),0,10)",
          "DPortion2": "=substring(@(1,Date),11,19)",
          "Date": "=concat(@(1,DPortion1),' ',@(1,DPortion2))" // overwrite the Date's value by portions 
        }
      },
      { // get rid of the DPortion parameters
        "operation": "remove",
        "spec": {
          "DPortion*": ""
        }
      }
    ]