Chethan
Chethan

Reputation: 73

Convert Multiple List Of Strings to different objects in Jolt

I have multiple list of strings in my request array. I have to iterate each list and take its first elements and form an object as bucketList similarly second elements of each list and form another object. How can be this achieved in jolt?

Request Packet:

{
  "recordSet": [
    {
      "id": "123",
      "bucketIdArray": [ "M03", "M02" ],
      "bucketNameArray": [ "EmployeeBucket200", "EmployeeBucket100" ],
      "bucketUsageArray": [ "500000000", "5000" ],
      "postBucketValueArray": [ "5004000", "5000" ]
    },
    {
      "id": "456",
      "bucketIdArray": [ "M04" ],
      "bucketNameArray": [ "EmployeeBucket300" ],
      "bucketUsageArray": [ "500000000" ],
      "postBucketValueArray": [ "5004000" ]
    }
  ]
}

Response Expected:

{
  "datas": {
    "historyDetails": [
      {
        "historyId": "123",
        "bucketlist": [
          {
            "bucketId": "M03",
            "bucketName": "EmployeeBucket200",
            "bucketUsage": "500000000",
            "postBucketValue": "5004000"
          },
          {
            "bucketId": "M02",
            "bucketName": "EmployeeBucket300",
            "bucketUsage": "50000",
            "postBucketValue": "5004000"
          }
        ]
      },
      {
        "historyId": "456",
        "bucketlist": [
          {
            "bucketId": "M04",
            "bucketName": "EmployeeBucket300",
            "bucketUsage": "500000000",
            "postBucketValue": "5004000"
          }
        ]
      }
    ]
  }
}

Upvotes: 0

Views: 131

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65408

You can use this shift spec

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "id": "datas.historyDetails[&1].&",
          "*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
        }
      }
    }
  }
]

enter image description here

If the Array strings needed to be removed from the key names, then each key-value pair should explicitly be specified for them such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": "datas.historyDetails[&1].&",
          "bucketIdArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketId" },
          "bucketNameArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketName" },
          "bucketUsageArray": { "*": "datas.historyDetails[&2].bucketlist[&].bucketUsage" },
          "postBucketValueArray": { "*": "datas.historyDetails[&2].bucketlist[&].postBucketValue" }
        }
      }
    }
  }
]

Another option would be adding three more steps before our original shift spec in order to prevent hardcoding of each keys individually with Array suffix such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "*": {
            "$": "&2.&.key",
            "@": "&2.&.val"
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "*": {
          "key": "=split('Array',@(1,&))"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "val": "&.[&2].@(1,key[0])"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "id": "datas.historyDetails[&1].&",
          "*": { "*": "datas.historyDetails[&2].bucketlist[&].&1" }
        }
      }
    }
  }
]

enter image description here

Upvotes: 1

Related Questions