1

I have an array of JSON Objects that I need to transform into a new array of JSON Objects that align with my schema. I am new to Jolt and my output is not what I need.

I tried multiple ways but not able to get my desired result.

This is my Input JSON: It has 2 exactly same entries of Joe Hashashi in product Yearly and Leo Biz in product Monthly.

{
  "totalApplication": {
    "applicationName": "MSOffice",
    "source": "EMAIL",
    "products": [
      {
        "productName": "Yearly",
        "userList": [
          {
            "userId": "3544353",
            "email": "[email protected]",
            "userName": "Joe Hashashi",
            "lastActive": "1705088660"
          },
          {
            "userId": "3544353",
            "email": "[email protected]",
            "userName": "Joe Hashashi",
            "lastActive": "1705088660"
          },
          {
            "userId": "3544354",
            "email": "[email protected]",
            "userName": "Jacob Martin",
            "lastActive": "1705088661"
          }
        ]
      },
      {
        "productName": "Monthly",
        "userList": [
          {
            "userId": "4545455",
            "email": "[email protected]",
            "userName": "Amanda Kore",
            "lastActive": "1705088662"
          },
          {
            "userId": "4545456",
            "email": "[email protected]",
            "userName": "Leo Biz",
            "lastActive": "1705088663"
          },
          {
            "userId": "4545456",
            "email": "[email protected]",
            "userName": "Leo Biz",
            "lastActive": "1705088663"
          }
        ]
      }
    ]
  }
}

This is the Jolt Spec I tried:

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "productUserId": "=concat(@(3,planName),'-',@(1,userId),'-',@(1,email),'-',@(1,userName),'-',@(1,lastActive))"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "userName": "@(1,productUserId).name",
                "email": "@(1,productUserId).email",
                "userId": "@(1,productUserId).userId",
                "productUserId": "@(1,productUserId).productUserId",
                "lastActive": "@(1,productUserId).additionalAttribute.lastActive",
                "@(2,productName)": "@(1,productUserId).additionalAttribute.productName",
                "@(4,applicationName)": "@(1,productUserId).additionalAttribute.applicationName"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": "[]"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "name": "[&1].name",
        "email": "[&1].email",
        "userId": "[&1].userId",
        "additionalAttribute": "[&1].additionalAttribute",
        "productUserId": null
      }
    }
  }
]

With above mentioned specs I am getting below result. My jolt spec adding the user details in the same object if 2 user objects have exactly same details. I want separate details even though if 2 user objects in the userList have same details. You can see the user with name Joe Hashashi and Leo Biz has 2 entries in the userList in my input json and after jolt transformation both details coming under same object in the response.

[ 
 {
   "name" : [ "Joe Hashashi", "Joe Hashashi" ],
   "email" : [ "[email protected]", "[email protected]" ],
   "userId" : [ "3544353", "3544353" ],
   "additionalAttribute" : {
     "productName" : [ "Yearly", "Yearly" ],
     "applicationName" : [ "MSOffice", "MSOffice" ],
     "lastActive" : [ "1705088660", "1705088660" ]
  }
 }, 
 {
   "name" : "Jacob Martin",
   "email" : "[email protected]",
   "userId" : "3544354",
   "additionalAttribute" : {
     "productName" : "Yearly",
     "applicationName" : "MSOffice",
     "lastActive" : "1705088661"
  }
 }, 
 {
   "name" : "Amanda Kore",
   "email" : "[email protected]",
   "userId" : "4545455",
   "additionalAttribute" : {
     "productName" : "Monthly",
     "applicationName" : "MSOffice",
     "lastActive" : "1705088662"
  }
 }, 
 {
   "name" : [ "Leo Biz", "Leo Biz" ],
   "email" : [ "[email protected]", "[email protected]" ],
   "userId" : [ "4545456", "4545456" ],
   "additionalAttribute" : {
     "productName" : [ "Monthly", "Monthly" ],
     "applicationName" : [ "MSOffice", "MSOffice" ],
     "lastActive" : [ "1705088663", "1705088663" ]
  }
 } 
]

I want separate details even if more than 2 user objects in the userList have exactly same details. Below is the desired output I want. You can see the user with name Joe Hashashi has comes in separate objects.

[
  {
    "additionalAttribute": {
      "productName": "Yearly",
      "applicationName": "MSOffice",
      "lastActive": "1705088660"
    },
    "userId": "3544353",
    "email": "[email protected]",
    "name": "Joe Hashashi"
  },
  {
    "additionalAttribute": {
      "productName": "Yearly",
      "applicationName": "MSOffice",
      "lastActive": "1705088660"
    },
    "userId": "3544353",
    "email": "[email protected]",
    "name": "Joe Hashashi"
  },
  {
    "additionalAttribute": {
      "productName": "Monthly",
      "applicationName": "MSOffice",
      "lastActive": "1705088662"
    },
    "userId": "4545455",
    "email": "[email protected]",
    "name": "Amanda Kore"
  },
  {
    "additionalAttribute": {
      "productName": "Monthly",
      "applicationName": "MSOffice",
      "lastActive": "1705088663"
    },
    "userId": "4545456",
    "email": "[email protected]",
    "name": "Leo Biz"
  },
  {
    "additionalAttribute": {
      "productName": "Yearly",
      "applicationName": "MSOffice",
      "lastActive": "1705088661"
    },
    "userId": "3544354",
    "email": "[email protected]",
    "name": "Jacob Martin"
  },
  {
    "additionalAttribute": {
      "productName": "Monthly",
      "applicationName": "MSOffice",
      "lastActive": "1705088663"
    },
    "userId": "4545456",
    "email": "[email protected]",
    "name": "Leo Biz"
  }
]

Its not giving desired output. Can anyone help me with this?

1 Answer 1

0

You can group by the indexes of the "userList" array as well, through adding &1 symbols such as

[
  {
    "operation": "modify-default-beta",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "productUserId": "=concat(@(3,planName),'-',@(1,userId),'-',@(1,email),'-',@(1,userName),'-',@(1,lastActive))"
              }
            }
          }
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "totalApplication": {
        "products": {
          "*": {
            "userList": {
              "*": {
                "email|userId": "@1,productUserId.&1.&",
                "userName": "@1,productUserId.&1.name",
                "productUserId|lastActive": "@1,productUserId.&1.additionalAttribute.&",
                "@2,productName": "@1,productUserId.&1.additionalAttribute.productName",
                "@4,applicationName": "@1,productUserId.&1.additionalAttribute.applicationName"
              }
            }
          }
        }
      }
    }
  },
  {//get rid of the object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[]"
      }
    }
  }
]
Sign up to request clarification or add additional context in comments.

Comments

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.