Expand my Community achievements bar.

Do you have questions about the migration to Adobe Business Platform? Come join our upcoming coffee break and ask away!
SOLVED

Merging two json collections in fusion

Avatar

Employee

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.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

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

 

Check out the sample code attached, that should work.

View solution in original post

8 Replies

Avatar

Level 9

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

Avatar

Employee

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

Avatar

Community Advisor

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

Avatar

Employee

Hi @ChrisStephens ,

when we merge two collections we get this output result: [{"key1":"value1"},{"key2":"value2"},{"key3":"value3"}] . If we use get(result[];1) we get the only first object  {"key1":"value1"}  

Avatar

Employee

Satya_NarayanaMu_0-1714057164262.png

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

Avatar

Correct answer by
Community Advisor

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

 

Check out the sample code attached, that should work.