Search code examples
jsonapache-nifijolt

How to replace values in a JSON using JOLT Notation


Input JSON :

{
  "sid": "1",
  "poste": "MapRecord[{altitude=473.0, latitude=49.92665, poste=Libramont, longitude=5.36080}]",
  "mtime": "2023-07-12T01:00:00Z",
  "tsa": "17.2",
  "hra": "90.2",
  "tsf": "15.8",
  "tss": "21.4",
  "ens": "0.0",
  "dvt": "319.6",
  "vvt": "0.9",
  "plu": "0.0",
  "hct": "60.0"
}

Output JSON

{
  "Date": "2023-07-12 01:00:00",
  "stationname": "Libramont",
  "data": [
    {
      "code": "A1-T1",
      "value": "17.2"
    },
    {
      "code": "A1-ETP1",
      "value": "16.2"
    },
    {
      "code": "A1-HR1",
      "value": "90.2"
    },
    {
      "code": "A1-TSF",
      "value": "15.8"
    },
    {
      "code": "A1-P1-T1",
      "value": "21.4"
    },
    {
      "code": "A1-PY1",
      "value": "0.0"
    },
    {
      "code": "A1-DVT",
      "value": "319.6"
    },
    {
      "code": "A1-WS1",
      "value": "0.9"
    },
    {
      "code": "A1-R1",
      "value": "0.0"
    },
    {
      "code": "A1-HCT",
      "value": "60.0"
    }
  ]
}

Following JOLT works fine except I need my code name values to be replaced by different names as in the output

mapping values

What I have done so far, all are working except I need to map the correct code names accordingly as show in the attached picture

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "po1": "=split('poste=',@(1,poste))",
      "po2": "=lastElement(@(1,po1))",
      "po3": "=split(',',@(1,po2))",
      "stationname": "=firstElement(@(1,po3))"
    }
  },
  {
    "operation": "remove",
    "spec": {
      "po*": ""
    }
  },
  {
    "operation": "shift",
    "spec": {
      "mtime": "Date",
      "sta*": "&",
      "sid": "&",
      "*": "data.&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "Date|stationname": "&",
      "data": {
        "*": {
          "$": "&2[#2].code",
          "@": "&2[#2].value"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "data": {
        "*": {
          "value": [
            "=notNull",
            0
          ]
        }
      },
      "DPortion1": "=substring(@(1,Date),0,10)",
      "DPortion2": "=substring(@(1,Date),11,19)",
      "Date": "=concat(@(1,DPortion1),' ',@(1,DPortion2))"
    }
  },
  { // get rid of the DPortion parameters 
    "operation": "remove",
    "spec": {
      "DPortion*": ""
    }
  }
]

Solution

  • You can convert the first shift transformation spec to this one :

      {
        "operation": "shift",
        "spec": {
          "mtime": "Date",
          "sta*": "&",
          "sid": "&",
          "tsa": "data.A1-T1",
          "hra": "data.A1-HR1",
          "tsf": "data.A1-TSF",
          "tss": "data.A1-P1-T1",
          "vvt": "data.A1-WS1",
          "ens": "data.A1-PY1",
          "plu": "data.A1-R1",
          "dvt": "data.A1-DVT",
          "tha": "data.A1-ETP1",
          "hct": "data.A1-HCT"
        }
      }
    

    where renaming occur by the literals stated on the right hand side, for those attributes respectively.