Combining two arrays into key-value pairs

Hi there!

I have a scenario where I fetch data from an API, the response is essentially an array of objects:

I wanted to work on the “all” array within “attributes”, the end goal is to have a collection that looks like this:

{
  "vehicletype": "Used"
}

And so on.

I am not sure if there’s a better way to go on about this, but I have separated the keys (canonicalName), and the values into their own arrays.

Now, I am wondering if there is a way to combine the two arrays into a single object where the each element of the Keys array become a key to its respective element in the Values array. Kind of like zip() function in Python.

API response could return about 70-80 elements, and each element has about 20-21 attributes, so using an iterator would skyrocket my operations consumption, if I understand correctly.

Thank you!

Hi @Youssef

What is the required final JSON structure?

Hi Ponvaskon,

I’d like the final output to be like this:

{
"vehicletype": "Used",
"carmilageinkms": "85000",
"carmake": "Land Rover"
}

etc.

Welcome to the Make community!

To do this, you can use the built-in array function toCollection to convert an array into a collection

{{ toCollection(array; key; value) }}

e.g.: (paste this into the field)

{{ toCollection(1.listings.1.attributes.all; "canonicalName"; "values") }}

For more information, the function’s documentation can be found in the Help Centre. You should also complete the tutorials in the Make Academy.

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

Thank you so much @samliew, this is perfect, I adjusted just a little bit because “values” is an array and I wanted the first element of it, so I made it like this:

One last thing if I may, the listings is an array of objects, like this:

Is there a way I could do what I just did above but to N elements within the listings array? So in the end I would have a structure that holds all data about the vehicles returned in the API response?

For example, if the API returns two vehicles, I would like to have this:

// First car
{"carmake": "Land Rover", "caryear": "2008"},

// Second car
{"carmake": "Audi", "caryear": "2015"}

Is this possible?

Welcome to the Make community!

“Looping” Through Array Items

When you see an ARRAY, think ITERATOR module.

In the Iterator module, MAP the entire array within it:

{{ 25.data.data.profile.listings }}

Then, you can use the toCollection function on each listing:

{{ toCollection(26.attributes.all; "canonicalName"; "values.1" }}

Example

Here is an example of how your scenario could look:
Screenshot_2025-03-22_200344 (2)

This is just an example. Your final solution may or may not look like this depending on your requirements and actual input data.

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.

This works beautifully, thank you. However, I am likely to have a lot of responses (elements) within the array, I think using an iterator is going to consume a lot of operations. I was wondering if it were possible to do this without an iterator and just have a single structure which contains different vehicle data in it. I realize this may not be possible, just wanted to explore different options for optimizations sake.

Yes there is a very advanced method, and it may take a few hours to create, so I will not be suggesting it here.

You can also use the Hire a Pro category to request for private 1-to-1 assistance via video call/screenshare/private messaging/etc. This may help you get your issue resolved faster especially if it is urgent or contain sensitive information. It is important to post your request in the Hire a Pro category, as forum members are not allowed to advertise their services in other categories like here (even if it’s free/unpaid). Posting in the Hire a Pro category will allow other members to assist you over other forms of communication.

Alternatively, you can use the private messaging feature to directly reach out to other forum members. To do this, go to your profile, and click on the “New Message” button:

Hope this helps! Let me know if there are any further questions or issues.

@samliew

P.S.: Investing some effort into the Make Academy will save you lots of time and frustration using Make.