1

Here I want to remove duplicate accounts with the help of account_id for all accounts in this JSON file. There are 12 accounts in this JSON file need to duplicate accounts to make them unique. someone, Please help me resolve this.

Here total 12 accounts in input JSON payload

Input JSON:

             {
            "Accounts" : [

     {                                 
      "account_id" :"c93c9cc0-26b4-11ed-a261-0242ac120002",
      "account_Number" : 458554,
      "account_name": "Ramu"
      },
       {  
       "account_id" : "e4b0efdc-26b5-11ed-a261-0242ac120002",
            "accountNumber" : 741852,
            "account_name": "Rajesh"
             },
            {

        "account_id" : "027aa0f8-26b6-11ed-a261-0242ac120002",
           "accountNumber" : 963258,
           "account_name": "Harsha"
           },
           {

    "account_id" : "09ebdd0c-26b6-11ed-a261-0242ac120002",
         "accountNumber" : 852456,
         "account_name": "Vamsi"
           }
           ],
         "address" : [
         {
          "street" : "XXXX",
          "state"  : "XXXX",
          "country": "XXXX"
          }
          ],
         "Accounts" : [
          {
   "account_id" : "f4974e1e-26b5-11ed-a261-0242ac120002",
           "accountNumber" : 246598,
           "account_name": "Indu"
           },
           {

          "account_id" :"2fa15b30-26b6-11ed-a261-0242ac120002",
          "accountNumber" : 789789,
          "account_name": "Suresh"
          },                              
          {
       "account_id" : "c93c9cc0-26b4-11ed-a261-0242ac120002",
           "accountNumber" : 458554,
           "account_name": "Ramu"
           },
           {
       "account_id" : "e4b0efdc-26b5-11ed-a261-0242ac120002",
             "accountNumber" : 741852,
             "account_name": "Rajesh"
           }
           ],
           "PhoneNumbers" :[
                {
                 "phcountry" : "XXXX",
                 "phno" : "XXXX"
                 }
                 ],
          "Accounts" :[
        {
         "account_id" : "09ebdd0c-26b6-11ed-a261-0242ac120002",
         "accountNumber" : 852456,
          "account_name": "Vamsi"
         },
         {
          "account_id" : "c93c9cc0-26b4-11ed-a261-0242ac120002",
          "account_Number" : 458554,
          "account_name": "Ramu"
          },
          {
        "account_id" : "2ad45a96-b907-4e2e-ae90-4f429c3fc0e4",
               "accountNumber" : 741852,
               "account_name": "Savitri"
           },
           {
     "account_id" :"f4974e1e-26b5-11ed-a261-0242ac120002",
           "accountNumber" : 246598,
           "account_name": "Indu"
           }
           ]
           }

Finally, I NEED the output of unique accounts along with other data present in this JSON file. thank you

Here total of 7 unique accounts in output payload Expected output:

     {
            "Accounts" : [

     {                                 
      "account_id" :"c93c9cc0-26b4-11ed-a261-0242ac120002",
      "account_Number" : 458554,
      "account_name": "Ramu"
      },
       {  
       "account_id" : "e4b0efdc-26b5-11ed-a261-0242ac120002",
            "accountNumber" : 741852,
            "account_name": "Rajesh"
             },
            {

        "account_id" : "027aa0f8-26b6-11ed-a261-0242ac120002",
           "accountNumber" : 963258,
           "account_name": "Harsha"
           },
           {

    "account_id" : "09ebdd0c-26b6-11ed-a261-0242ac120002",
         "accountNumber" : 852456,
         "account_name": "Vamsi"
           }
           ],
         "address" : [
         {
          "street" : "XXXX",
          "state"  : "XXXX",
          "country": "XXXX"
          }
          ],
         "Accounts" : [
          {
   "account_id" : "f4974e1e-26b5-11ed-a261-0242ac120002",
           "accountNumber" : 246598,
           "account_name": "Indu"
           },
           {

          "account_id" :"2fa15b30-26b6-11ed-a261-0242ac120002",
          "accountNumber" : 789789,
          "account_name": "Suresh"
          }
           ],
           "PhoneNumbers" :[
                {
                 "phcountry" : "XXXX",
                 "phno" : "XXXX"
                 }
                 ],
          "Accounts" :[
          {
        "account_id" : "2ad45a96-b907-4e2e-ae90-4f429c3fc0e4",
               "accountNumber" : 741852,
               "account_name": "Savitri"
           },
           ]
           }
                        
                        
                                                  
                                                
Buddy
  • 11
  • 3
  • Assuming the JSON is the input, you have to show the expected output too. – aled Aug 28 '22 at 12:12
  • Ok I will add expected output too.Thanks for the comment – Buddy Aug 28 '22 at 12:43
  • Please assume that the output payload is only unique accounts and other data like addres and phno.please help me resolve this.Thank you – Buddy Aug 28 '22 at 12:59
  • The input and output are not valid JSON. Please fix. – aled Aug 28 '22 at 13:55
  • Could you please assume the payloads as input and output like above or assume same type of example payloads to resolve this problem.Thank you – Buddy Aug 28 '22 at 14:24
  • And please clarify the criteria to understand an account is unique. You should provide all details in your question to be able to understand and reproduce the issue. Read https://stackoverflow.com/help/how-to-ask for more information about how to ask questions in Stackoverflow.com. – aled Aug 28 '22 at 14:25
  • my intention is to remove the duplicates from the JSON having three same arrays like "accounts" in one JSON file.We need to remove the duplicate accounts with the help of account_id.Thank you – Buddy Aug 28 '22 at 14:27
  • You'll need to be more explicit in the explanation of what is a duplicate and what is not. Also you need to provide valid inputs and outputs. – aled Aug 28 '22 at 14:42
  • Now JSONS are completely valid. Please help me resolve this. Thank you – Buddy Aug 28 '22 at 14:55
  • In the input payload, some duplicate accounts are present u can observe the same account_id, account name, and account_Number. We need to remove those accounts.Thank you – Buddy Aug 28 '22 at 14:57
  • That's not clear enough unfortunately. By present you mean in a previous Accounts array? In the same? Later? – aled Aug 28 '22 at 15:10
  • combine both 3 arrays and remove the duplicate accounts. we can find it easily by the same account id.Thank you – Buddy Aug 28 '22 at 15:31
  • If I do that then the answer will not match your expected output. That's the reason you need to clarify the logic for removing duplicates. – aled Aug 28 '22 at 15:38
  • Ok no problem .If you did like combining arrays and than remove the duplicates.There is no need to match with output payload.Please help me complete this.Thank you – Buddy Aug 28 '22 at 15:44

1 Answers1

0

I will assume that the logic to remove duplicates is to remove any element from Accounts keys where their account_id is already present in an element in a previous Accounts key in the same JSON document. I created a script that looks for each Accounts key, finds the unique accounts_ids in previous elements and uses them to filter the current array. It doesn't not remove duplicates inside the same array, however that should be easy to add.

%dw 2.0
output application/json
fun unique_accounts_until_index(o, i)=flatten((o filterObject ((value, key, index) -> key as String == "Accounts" and index < i )).*Accounts).account_id distinctBy $
---
payload mapObject ((value, key, index) -> 
    (key):  if (key as String == "Accounts") 
                value filter ( !( unique_accounts_until_index(payload, index) contains log("id", $.account_id)))
            else value
)

Output for the input in the question:

{
  "Accounts": [
    {
      "account_id": "c93c9cc0-26b4-11ed-a261-0242ac120002",
      "account_Number": 458554,
      "account_name": "Ramu"
    },
    {
      "account_id": "e4b0efdc-26b5-11ed-a261-0242ac120002",
      "accountNumber": 741852,
      "account_name": "Rajesh"
    },
    {
      "account_id": "027aa0f8-26b6-11ed-a261-0242ac120002",
      "accountNumber": 963258,
      "account_name": "Harsha"
    },
    {
      "account_id": "09ebdd0c-26b6-11ed-a261-0242ac120002",
      "accountNumber": 852456,
      "account_name": "Vamsi"
    }
  ],
  "address": [
    {
      "street": "XXXX",
      "state": "XXXX",
      "country": "XXXX"
    }
  ],
  "Accounts": [
    {
      "account_id": "f4974e1e-26b5-11ed-a261-0242ac120002",
      "accountNumber": 246598,
      "account_name": "Indu"
    },
    {
      "account_id": "2fa15b30-26b6-11ed-a261-0242ac120002",
      "accountNumber": 789789,
      "account_name": "Suresh"
    }
  ],
  "PhoneNumbers": [
    {
      "phcountry": "XXXX",
      "phno": "XXXX"
    }
  ],
  "Accounts": [
    {
      "account_id": "2ad45a96-b907-4e2e-ae90-4f429c3fc0e4",
      "accountNumber": 741852,
      "account_name": "Savitri"
    }
  ]
}
aled
  • 21,330
  • 3
  • 27
  • 34