0

I have this JSON

[{"id":7,"serial":"7bc530","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":8,"serial":"4a18d27","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":9,"serial":"f30ef","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":10,"serial":"9e6d","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":11,"serial":"4d8665a3","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null},
{"id":12,"serial":"4fe1457","randomDouble":0.0,"randomDouble2":0.0,"randomDouble3":0.0,"date":1352228474000,"removed":null}]

and I have this JSON

{"computers":[{"id":"7bc530","name":"Dell","description":"Dell"},
{"id":"f30ef","name":"HP","description":"HP"},
{"id":"9e6d","name":"Compaq","description":"Compaq"},
{"id":"4d8665a3","name":"Toshiba","description":"Toshiba"},
{"id":"4fe1457","name":"Asus","description":"Asus"},
{"id":"4a18d27","name":"Acer","description":"Acer"}]}

I want to replace the "serial" element in the first JSON with the "Description" in this one. The reason why I need it in one JSON is that I am using a DataTable and I can only pass one JSON in.

I'm not sure how I can do this in Javascript / JQuery?

4
  • 1
    i'm guessing serial in 1 is id in 2, so loop through the first one referencing the second one to populate the first one. Commented Nov 6, 2012 at 21:54
  • are both arrays in the same order? Commented Nov 6, 2012 at 21:59
  • tip: iterate over the first one and use the value of the serial key to find the entry in the second one ... I leave the implementation up to you to not intervene @undefined 's question ;-) Commented Nov 6, 2012 at 21:59
  • I've tried a few things but I'm not that good with javascript / jquery syntax to have it actually work properly :( Commented Nov 7, 2012 at 13:21

2 Answers 2

1

You can accomplish this without any jQuery by setting up small function:

(see the demo fiddle)

function replaceSerial (data1, data2) {
    var descs = {}, computers = data2['computers'], final = data1;

    for (var i = 0; i < computers.length; i++ ) {
        descs[computers[i]['id']] = computers[i]['description'];
    }

    for (var i = 0; i < data1.length; i++) {
        final[i]['serial'] = descs[data1[i]['serial']];
    }

    return final;
}

Then just save your two pieces of JSON into variables and invoke the function:

var json1, json2, mergedJson;

json1 = // DATA IN FIRST JSON;
json2 = // DATA IN SECOND JSON;

mergedJson = replaceSerial (json1, json2);
Sign up to request clarification or add additional context in comments.

3 Comments

What do I do if my "data1" json has no name? The name "computers" is available on the second but none for the first.
I'm not sure I understand your question. Can you be more specific, or post an example? You can assign a "name" to your json by assigning it to a variable. JSON is by-nature valid javascript. The first piece of JSON you posted is an array [], and the second you posted is an object {}.
Hey please let me know, how to merge two JSON object into one?
0

Assuming your first object is called to and the second object is called from

// Iterate over each entry in to
to.forEach(function(value) {
    // In each iteration find elements in from where the id is the same
    // as the serial of the current value of to
    var description = from.computers.filter(function(element){
        if (element.id == value.serial) return true;
    });
    // Copy description of first found object in the description property of
    // the current object
    value.description = description[0].description;
    // Unset serial?
    delete value.serial;
});

DEMO

2 Comments

should note that forEach not available in all browsers such as IE < 9
And in some browsers there is no JavaScript such as lynx ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.