Home > Mobile >  Convert Flat json to Nested Json with multiple arrays using Jolt transform
Convert Flat json to Nested Json with multiple arrays using Jolt transform

Time:08-31

I'm trying to write a spec to do the below transformation using jolt transformation. I need to convert the flat json to nested Json

I am having some trouble with converting the flat JSON to Nested JSON. I have looked at examples and didn't get any closer as to what is mentioned above. I need to transform a JSON structure by using a JOLT spec. I use https://jolt-demo.appspot.com to test the following below.

Input :

[
  {
    "container_id": "a",
    "carrier_scac": "b",
    "location": "banglore",
    "state": "karnataka",
    "country": "India"
  },
  {
    "container_id": "a",
    "carrier_scac": "b",
    "location": "pune",
    "state": "maharashtra",
    "country": "India"
  },
  {
    "container_id": "c",
    "carrier_scac": "d",
    "location": "dharwad",
    "state": "kan",
    "country": "India"
  },
  {
    "container_id": "c",
    "carrier_scac": "d",
    "location": "hubli",
    "state": "kant",
    "country": "India"
  }
]

Desired Output:

[
  {
    "load": {
      "container_id": "a",
      "carrier_scac": "b",
      "stops": [
        {
          "location": "banglore",
          "state": "karnataka"
        },
        {
          "location": "pune",
          "state": "maharashtra"
        }
      ]
    },
    "containerInfo": {
      "country": "India"
    }
  },
  {
    "load": {
      "container_id": "c",
      "carrier_scac": "d",
      "stops": [
        {
          "location": "dharwad",
          "state": "kan"
        },
        {
          "location": "hubli",
          "state": "kant"
        }
      ]
    },
    "containerInfo": {
      "country": "India"
    }
  }
]

Jolt Spec that I'm using :

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "container_id": "@(1,container_id).&",
        "carrier_scac": "@(1,container_id).&",
        "*": "@(1,container_id).stops[&1].&"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "container_id": "ONE",
        "carrier_scac": "ONE"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]

CodePudding user response:

Current spec is pretty good, just needs some little modifications such as adding load and containerInfo nodes, and shortening a bit as below

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "@(1,container_id).load.stops[&1].&", // "else" case
        "country": "@(1,container_id).load.containerInfo.&",
        "c*": "@(1,container_id).load.&" // the attributes starting with "c" but other than "country" 
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=recursivelySquashNulls"
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "*": {
          "c*": "ONE", 
          "containerInfo": {
            "*": "ONE"
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": ""
    }
  }
]
  • Related