Avatar of tradeline
tradeline

asked on 

Merge 2 JSON strings??

I have the following function, that is merging 2 JSON strings.  It's working for the most part, except that It's not currently handling adjacent properties of each object.

So, for example in the code below, the first object has an Address object with several properties, and the second object has that same Address object with different properties.

Currently, the properties of the first object are getting overriden, instead of merged.

If the properties are the same in both objects, then they can be overridden (no duplicates), but if they're different, then I'd like them to be appended...is this simple ??


var firstObj = { "Type":"1", "Company": {"CompanyAddresses":[ {"Address": {"PostalCode":"V6H1S2", "AddressLine1":"3318 Main"}}]} };
                var secondObj = { "Type":"1", "Company": {"CompanyAddresses":[ {"Address": {"AddressLine2":"#2"}}]} }
                //var secondObj = { "ContractorName":"My Name", "Company": {"Name":"My Company Rocks"} };
                function merger(a,b)
                {
                    for( i in a )
                    {
                            for( j in b )
                            {
                                    if(i==j)
                                    {
                                            for( k in b[j] )
                                            {
                                                    a[i][k] = b[j][k];
                                            }
                                    }
                            }
                    }
                }
                merger(firstObj,secondObj);
                alert(firstObj.Company.CompanyAddresses[0].Address.AddressLine1);

Open in new window

JavaScriptJScript

Avatar of undefined
Last Comment
b0lsc0tt
ASKER CERTIFIED SOLUTION
Avatar of tradeline
tradeline

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of b0lsc0tt
b0lsc0tt
Flag of United States of America image

Does that mean you found an answer or do you still need help with this?  If you have an answer then please close this.  Let me know if you have a question about doing it but you should be able to just click on Accept as Solution in the last comment you posted.  If you do need help still then let us know and maybe clarify the last post.

bol
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo