Skip to main content
Adobe Employee
Apr 25, 2024 8:47 AM UTC
Solved

Merging two json collections in fusion

I'm facing one challenge while merging two JSON collections. For example collection1--> {"key1":"value1","key2":"value2"} and Collection2-->{"key3":"value3"}. I need output like {"key1":"value1","key2":"value2","key3":"value3"}. I tried by converting this object into an array and merging these but I'm getting an array of objects like [{"key1":"value1"},{"key2":"value2"},{"key3":"value3"}]. I need all the elements in a single object. Please let me know if anyone is facing the same challenge and if you have a solution, please share it. I really appreciate any help you can provide.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
ChrisStephens
ChrisStephensAccepted solution
Community Advisor
Apr 25, 2024 3:27 PM UTC

sure @chrisstephens  as mentioned above screenshot After the merge I got the array of objects


oh, that's not what I was picturing, sorry.

 

Check out the sample code attached, that should work.

9 replies

lgaertner
Level 9
Apr 25, 2024 1:13 PM UTC

Hello Satya,

 

your approach is correct. Using:

{{merge(toArray(4.firstVar); toArray(4.secondVar))}}

 

gives me

{ "result": [ { "key1": "value1" }, { "key2": "value2" }, { "key3": "value3" } ] }

 

Regards

Lars

Translate
Adobe Employee
Apr 25, 2024 1:18 PM UTC

Hi @lgaertner ,

Thanks for your response. yeah, I'm also using the same expression. But I need this output {"key1":"value1","key2":"value2","key3":"value3"}. All keys and values in a single object.

 

Thanks,

Satya v

Translate
ChrisStephens
Community Advisor
Community Advisor
Apr 25, 2024 2:19 PM UTC

Once you merge everything like that, why not just do get({your array};0)

Translate
AndyHess
Adobe Employee
Adobe Employee
Oct 23, 2024 1:10 PM UTC

Hi All,

There is a new feature just released in Fusion that will manage this requirement much better.

We’re excited to announce the addition of a new function called mergeCollections under General Functions 

 

This function allows you to seamlessly merge two collections into one by combining their key-value pairs, making your data manipulation even more efficient

Translate