How to create a key-value object/collection, and add to an associative array containing key-value pairs

Hi,
With Make I can create an associative array based on a collection.
Also I can add to a “normal” array via the “add()” function.
But could not find in the documentation how to add a “key => value” entry to an associative array.

Is it possible, can you help me with that? Thank you.

Welcome to the Make community!

Could you provide examples of what you want to achieve?

When you say “normal”, did you mean an array containing primitive values? (i.e.: none of the items are collections or arrays)

If I understand your question, you have an array of collections:

Screenshot_2024-02-05_140218

You want to add a new collection in the same format to the array using the add function, is this correct?

{ 
  "key": "key4",
  "value": "value4"
}
3 Likes

I use the Parse JSON module in the JSON app to create a more complex data structure. This gives you complete control of the array, key/value structure. Then when it executes it will parse the JSON and create an array with the key value pairs you want. If you create multiple structues under the top array then those wil be generated as output bundles.

You can aggregate these outputs into one array using an array aggregator and get the structure you want then.

4 Likes

One way of doing that is use the Parse JSON module to create the new object (collection)

Screenshot_2024-02-05_140216

Then you can add the new collection to the array

Screenshot_2024-02-05_140231

Output

Screenshot_2024-02-05_140255

@alex.newpath sorry for posting this reply after you, but I already had it drafted ready to go while awaiting user response

4 Likes

Thank you @samliew this was exactly what I was looking for! :smiling_face:

2 Likes

No problem, glad I could help!

Quick tip: Did you know, you can convert a collection to key-value pairs using the built-in function toArray? The inverse of that is toCollection! These two advanced functions are not commonly known and you might find yourself using them especially if you are adding/modifying KVPs.

1. If you have a new question in the future, please start a new thread. This makes it easier for others with the same problem to search for the answers to specific questions, and you are more likely to receive help since newer questions are monitored closely.

2. The Make Community guidelines encourages users to try to mark helpful replies as solutions to help keep the Community organized.

This marks the topic as solved, so that:

others can save time when catching up with the latest activity here, and

  • allows others to quickly jump to the solution if they come across the same problem

To do this, simply click the checkbox at the bottom of the post that answers your question:
Screenshot_2023-10-04_161049

3. Don’t forget to like and bookmark this topic so you can get back to it easily in future!

2 Likes