Home > Blockchain >  How to remove one set of "{}" from the output of JOLT transform array for each item and ad
How to remove one set of "{}" from the output of JOLT transform array for each item and ad

Time:12-14

Working with Nifi JoltTransformJSON processor, fairly new to using JOLT Specs and can't figure out how to move a set of {} from each item of an array to a single set of {} around all items of the array form the output JSON. Please see the OUTPUT and the NEEDED OUTPUT sections below for comparisons. I tried creating the spec from reading the JOLT definition and some examples which got me pretty close, but this last part has been kicking my rear for a while, any hints would be awesome!

INPUT JSON:

{
  "AssetID": "1",
  "AssetNumber": "2",
  "AssetMaterial": "Cisco MDS 9706",
  "RackUnits": "9.0",
  "MaterialType": "Chassis",
  "AssetName": "Cisco-MDS-9706_1",
  "CustRID": "A001",
  "SerialNumber": "OU812",
  "Room": "ROOM5",
  "Datacenter": "DC69",
  "UMountingID": "86",
  "CabinetAssetID": "181",
  "CabinetName": "CAB666"
}

CURRENT SPEC:

[
  {
    "operation": "shift",
    "spec": {
      "AssetID": "data[].6.value",
      "AssetNumber": "data[].7.value",
      "AssetMaterial": "data[].8.value",
      "AssetName": "data[].9.value",
      "CustRID": "data[].10.value",
      "SerialNumber": "data[].11.value",
      "Room": "data[].12.value",
      "Datacenter": "data[].13.value",
      "UMountingID": "data[].14.value",
      "CabinetAssetID": "data[].15.value",
      "CabinetName": "data[].16.value"
    }
  },
  {
    "operation": "default",
    "spec": {
      "to": "table1"
    }
  },
  {
    "operation": "default",
    "spec": {
      "fieldsToReturn": [6, 7, 8, 9, 10, 11, 12]
    }
  }
]

OUTPUT JSON:

{
  "data": [
    {
      "6": {
        "value": "1"
      }
    },
    {
      "7": {
        "value": "2"
      }
    },
    {
      "8": {
        "value": "Cisco MDS 9706"
      }
    },
    {
      "9": {
        "value": "Cisco-MDS-9706_1"
      }
    },
    {
      "10": {
        "value": "A001"
      }
    },
    {
      "11": {
        "value": "OU812"
      }
    },
    {
      "12": {
        "value": "ROOM5"
      }
    },
    {
      "13": {
        "value": "DC69"
      }
    },
    {
      "14": {
        "value": "86"
      }
    },
    {
      "15": {
        "value": "181"
      }
    },
    {
      "16": {
        "value": "CAB666"
      }
    }
  ],
  "to": "table1",
  "fieldsToReturn": [ 6, 7, 8, 9, 10, 11, 12 ]
}

REQUIRED/EXPECTED OUTPUT:

{
  "data" : [
    {
        "6" : {
          "value" : "1"
        }, 
        "7" : {
          "value" : "2"
        }, 
        "8" : {
          "value" : "Cisco MDS 9706"
        }, 
        "9" : {
          "value" : "Cisco-MDS-9706_1"
        }, 
        "10" : {
          "value" : "A001"
        }, 
        "11" : {
          "value" : "OU812"
        }, 
        "12" : {
          "value" : "ROOM5"
        }, 
        "13" : {
          "value" : "DC69"
        }, 
        "14" : {
          "value" : "86"
        }, 
        "15" : {
          "value" : "181"
        }, 
        "16" : {
          "value" : "CAB666"
        }
    }
   ],
  "to" : "table1",
  "fieldsToReturn" : [ 6, 7, 8, 9, 10, 11, 12 ]
}

CodePudding user response:

What you need seems that the top level object should reside at the common index of the array data, so use index 0 such as data[0]. to combine those object as single object such as

[
  {
    "operation": "shift",
    "spec": {
      "AssetID": "data[0].6.value",
      "AssetNumber": "data[0].7.value",
      "AssetMaterial": "data[0].8.value",
       ...
       ...
      "#table1": "to"
    }
  },
  {
    "operation": "default",
    "spec": {
      "fieldsToReturn": [6, 7, 8, 9, 10, 11, 12]
    }
  },
  {
    "operation": "sort"
  }
]

btw, the first default transformation spec is superfluous, rather use

"#table1": "to"

within the shift transformation spec

  • Related