Community Articles
Find and share helpful community-sourced technical articles
Announcements
Alert: Welcome to the Unified Cloudera Community. Former HCC members be sure to read and learn how to activate your account here.
Labels (1)
Contributor

Short Description:

Using JOLT transformation to modify a JSON object.

Article:

In NIFI, JOLT Transformation can be used to help modifying a JSON object, the below code snippets are an example of how JOLT can be used to shift elements inside a JSON Object, in this case, the intention is to move and repeat "productionID" and "processID" inside of the array.

Original JSON Object:

{
  "productionID": "BHS0928MDB8391",
  "productionLine": [
    {
      "model": "Cooper",
      "year": 2018,
      "type": "Hatchback",
      "motorization": "Electric",
      "colour": "Midnight Black",
      "stageID": "MGOP94810482042"
    },
    {
      "model": "Cooper",
      "year": 2018,
      "type": "Hatchback",
      "motorization": "Diesel",
      "colour": "Silver",
      "stageID": "MGOP9183740194"
    }
  ],
  "processID": "884910421743POR4183UK"
}

Target JSON Object:

{
  "productionLine" : [ {
    "model" : "Cooper",
    "year" : 2018,
    "type" : "Hatchback",
    "motorization" : "Electric",
    "colour" : "Midnight Black",
    "stageID" : "MGOP94810482042",
    "productionID" : "BHS0928MDB8391",
    "processID" : "884910421743POR4183UK"
  }, {
    "model" : "Cooper",
    "year" : 2018,
    "type" : "Hatchback",
    "motorization" : "Diesel",
    "colour" : "Silver",
    "stageID" : "MGOP9183740194",
    "productionID" : "BHS0928MDB8391",
    "processID" : "884910421743POR4183UK"
  } ]
}

JOLT transformation code used:

[
  {
    "operation": "shift",
    "spec": {
      "productionLine": {
        "*": {
          "@": "[&1]",
          "@(2,productionID)": "[&1].productionID",
          "@(2,processID)": "[&1].processID"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "@": "productionLine"
      }
    }
  }
]

1,417 Views
Don't have an account?
Coming from Hortonworks? Activate your account here
Version history
Revision #:
1 of 1
Last update:
‎08-29-2018 08:24 AM
Updated by:
 
Contributors
Top Kudoed Authors