How to unify a list of products for a given customer dynamically?

Hello my friends.

How do I convert a json with this structure:

[
	{
		"sale_id": 868401,
		"date_payment": "2016-11-28 17:30:12",
		"sale_total": 699,
		"product_id": 100483,
		"product_name": "iPhone 13",
		"client_id": 100584,
		"client_name": "Brian Park",
		"client_email": "[email protected]"
	},
	{
		"sale_id": 606932,
		"date_payment": "2017-02-01 19:30:18",
		"product_id": 550309,
		"product_name": "MacBook Air M1",
		"sale_total": 999,
		"client_id": 100584,
		"client_name": "Brian Park",
		"client_email": "[email protected]"
	},
	{
		"sale_id": 909330,
		"date_payment": "2018-02-01 19:30:18",
		"product_id": 304592,
		"product_name": "iPad Air",
		"sale_total": 599,
		"client_id": 100584,
		"client_name": "Brian Park",
		"client_email": "[email protected]"
	},
	{
		"sale_id": 909330,
		"date_payment": "2018-02-01 19:30:18",
		"sale_total": 699,
		"product_id": 100483,
		"product_name": "iPhone 13",
		"client_id": 67593,
		"client_name": "Darren Walker",
		"client_email": "[email protected]"
	}
]


for this structure?
[
   {
      "client_id":100584,
      "client_name":"Brian Park",
      "client_email":"[email protected]",
      "products":[
         {
            "product_id":100483,
            "product_name":"iPhone 13"
         },
         {
            "product_id":550309,
            "product_name":"MacBook Air M1"
         },
         {
            "product_id":304592,
            "product_name":"iPad Air"
         }
      ]
   },
   {
      "client_id":67593,
      "client_name":"Darren Walker",
      "client_email":"[email protected]",
      "products":[
         {
            "product_id":100483,
            "product_name":"iPhone 13"
         }
      ]
   }
]

I need to unify in a list all the products purchased by the customer.

Can someone help me?

thanks a lot :slight_smile:

I did it but I don’t know if it’s the right way to do it. :thinking:

1 Like

Hi @rafaelpiton, welcome to the community :tada:

Your approach seems to work, so I’d say that’s perfectly good :slight_smile:

The final Code node would, however, run twice as it is called twice (once from the previous products node, once from your client node):

So depending on what you are doing after this node it could lead to certain actions taking place twice which is probably not what you had in mind originally. So maybe you want to add a Merge node preventing this? This would let you wait for both inputs before continuing, preventing this double-execution:

1 Like

Hello Tom! :slight_smile:

to solve this problem, I removed “product” node…
and in the “json” node, I pointed const products to the “Mock Data” node.

Apparently it solved the problem.

Do you think it’s the best approach?

thank you very much for the quick help!!! :slight_smile:

1 Like

Nice one, looking good to me! It’s also much easier to read than my suggestion with the Merge node :slight_smile:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.