Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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?

share|improve this question
3  
What have you tried? –  undefined Nov 6 '12 at 21:50
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. –  Kevin B Nov 6 '12 at 21:54
    
are both arrays in the same order? –  charlietfl Nov 6 '12 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 ;-) –  awenkhh Nov 6 '12 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 :( –  envinyater Nov 7 '12 at 13:21

2 Answers 2

up vote 1 down vote accepted

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);
share|improve this answer
    
What do I do if my "data1" json has no name? The name "computers" is available on the second but none for the first. –  envinyater Nov 7 '12 at 13:22
    
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 {}. –  Steven Schobert Nov 7 '12 at 15:32
    
I was able to get this to work, thank you :) –  envinyater Nov 7 '12 at 16:08
    
Hey please let me know, how to merge two JSON object into one? –  Udhaya Apr 18 '13 at 11:16

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

share|improve this answer
    
should note that forEach not available in all browsers such as IE < 9 –  charlietfl Nov 6 '12 at 22:28
    
And in some browsers there is no JavaScript such as lynx ;) –  clentfort Nov 6 '12 at 23:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.