2

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": {
      "*": ""
    }
  }
]

1 Answer 1

1

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": {
      "*": ""
    }
  }
]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks Barbaros, but it is not giving all the stops. It is giving only the first stop.
[ { "load" : { "container_id" : "a", "carrier_scac" : "b", "stops" : { "location" : "banglore", "state" : "karnataka" }, "containerInfo" : { "country" : "India" } } }, { "load" : { "container_id" : "c", "carrier_scac" : "d", "stops" : { "location" : "dharwad", "state" : "kan" }, "containerInfo" : { "country" : "India" } } } ]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.