VamsiKumar_gudala
VamsiKumar_gudala

Reputation: 41

Using jolt transform, converting to nested json for multiple payloads

Convert the flat json to nested json for the multiple payloads. I am having some trouble with converting the flat JSON to nested JSON. Here, i want to aggregate the data to stops and need to be aggregated for unique payloads. I use https://jolt-demo.appspot.com to test the following below.

input:

[
  {
    "container_id": "DEF_id",
    "haulType": "OL",
    "loadNumber": "DO123345",
    "billOfLading": "DO12345",
    "referenceNumbers": "LoadIDEF",
    "addressLine1": "DEF_address",
    "stopReferenceId": "0004",
    "stopType": "PL",
    "containerNumber": "454545"
  },
  {
    "container_id": "DEF_id",
    "haulType": "OL",
    "loadNumber": "DO123345",
    "billOfLading": "DO12345",
    "referenceNumbers": "LoadIDEF",
    "addressLine1": null,
    "stopReferenceId": "0003",
    "stopType": "PU",
    "containerNumber": "454545"
  },
  {
    "container_id": "ABC_id",
    "haulType": "IL",
    "loadNumber": "BO123345",
    "billOfLading": "BO12345",
    "referenceNumbers": "LoadID",
    "addressLine1": null,
    "stopReferenceId": "0002",
    "stopType": "PL",
    "containerNumber": "232323"
  },
  {
    "container_id": "ABC_id",
    "haulType": "IL",
    "loadNumber": "BO123345",
    "billOfLading": "BO12345",
    "referenceNumbers": "LoadID",
    "addressLine1": "ABC Street",
    "stopReferenceId": "0001",
    "stopType": "PU",
    "containerNumber": "232323"
  }
]

Expected Output:

[
  {
    "load": {
      "container_id": "DEF_id",
      "haulType": [
        "OL"
      ],
      "loadNumber": "DO123345",
      "billOfLading": "DO12345",
      "referenceNumbers": [
        "LoadIDEF"
      ],
      "stops": [
        {
          "addressLine1": "DEF_address",
          "stopReferenceId": "0004",
          "stopType": "PL"
        },
        {
          "addressLine1": null,
          "stopReferenceId": "0003",
          "stopType": "PU"
        }
      ]
    },
    "containerInfo": {
      "containerNumber": "454545"
    }
  },
  {
    "load": {
      "container_id": "ABC_id",
      "haulType": [
        "IL"
      ],
      "loadNumber": "BO123345",
      "billOfLading": "BO12345",
      "referenceNumbers": [
        "LoadID"
      ],
      "stops": [
        {
          "addressLine1": null,
          "stopReferenceId": "0002",
          "stopType": "PL"
        },
        {
          "addressLine1": "ABC Street",
          "stopReferenceId": "0001",
          "stopType": "PU"
        }
      ]
    },
    "containerInfo": {
      "containerNumber": "232323"
    }
  }
]

Here it is my jolt spec used

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "container_id": "@(1,containerNumber).load.&",
        "haulType": "@(1,containerNumber).load.&",
        "loadNumber": "@(1,containerNumber).load.&",
        "billOfLading": "@(1,containerNumber).load.&",
        "referenceNumbers": "@(1,containerNumber).load.&",
        "addressLine1": "@(1,containerNumber).load.stops[&1].&",
        "stopReferenceId": "@(1,containerNumber).load.stops[&1].&",
        "stopType": "@(1,containerNumber).load.stops[&1].&",
        "containerNumber": "@(1,containerNumber).containerInfo.&"
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "*": "ONE",
          "stops": "MANY"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "&",
        "load": {
          "haulType|referenceNumbers": "&1.&[]",
          "*": "&1.&"
        }
      }
    }
  }
]

Upvotes: 0

Views: 163

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65228

No need to individually write the attributes considering the expected result. You can partition by containerNumber values along with * and & wildcards to reperesent the key-value pairs of all attributes within the first spec. Then the separation of attributes(conditional logic) should be performed within the second spec in order to distinguish the display style of each key-value pairs such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,containerNumber).&"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "0": "&2.load.&1"
        },
        "haulType|referenceN*": {
          "0": "&2.load.&1[]" // 0 : pick only value of the first index from the array, &1[] : wrap up the values with square brackets
        },
        "addressLine1|stop*": {
          "*": "&2.load.stops[&].&1"
        },
        "containerN*": {
          "0": "&2.load.containerInfo.&1"
        }
      }
    }
  },
  {
    // get rid of object labels
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

the demo on the site is http://jolt-demo.appspot.com/ :

enter image description here

Upvotes: 1

Related Questions