1

I want to merge two JSON data using Javascript or Jquery.

var object1 = [{
    "id": 11,
    "name": "Vasanth",
    "username": "Vasanth.Rajendran",
    "email": "[email protected]",
    "address": {
      "street": "Nungampakkam",
      "suite": "No154",
      "city": "Chennai",
      "zipcode": "31428-2261",
      "geo": {
        "lat": "-38.2386",
        "lng": "57.2232"
      }
    },
    "phone": "024-648-3804",
    "website": "google.net",
    "company": {
      "name": "Test",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }];

var object2 = [{
    "id": 2,
    "name": "Raju",
    "username": "Raju.Rajendran",
    "email": "[email protected]",
    "address": {
      "street": "Nungampakkam",
      "suite": "No154",
      "city": "Chennai",
      "zipcode": "31428-2261",
      "geo": {
        "lat": "-38.2386",
        "lng": "57.2232"
      }
    },
    "phone": "024-648-3804",
    "website": "google.net",
    "company": {
      "name": "Test",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }];

example result:

[
  {
    "id": 1,
    "name": "Leanne Graham",
    "username": "Bret",
    "email": "[email protected]",
    "address": {
      "street": "Kulas Light",
      "suite": "Apt. 556",
      "city": "Gwenborough",
      "zipcode": "92998-3874",
      "geo": {
        "lat": "-37.3159",
        "lng": "81.1496"
      }
    },
    "phone": "1-770-736-8031 x56442",
    "website": "hildegard.org",
    "company": {
      "name": "Romaguera-Crona",
      "catchPhrase": "Multi-layered client-server neural-net",
      "bs": "harness real-time e-markets"
    }
  },
  {
    "id": 2,
    "name": "Ervin Howell",
    "username": "Antonette",
    "email": "[email protected]",
    "address": {
      "street": "Victor Plains",
      "suite": "Suite 879",
      "city": "Wisokyburgh",
      "zipcode": "90566-7771",
      "geo": {
        "lat": "-43.9509",
        "lng": "-34.4618"
      }
    },
    "phone": "010-692-6593 x09125",
    "website": "anastasia.net",
    "company": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  }];
2
  • 1
    Why are the names different from the input to the output? Commented Apr 25, 2015 at 7:55
  • Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. <script> var object1 = [{ "id": 1, "name": "Vasanth", "username": "Vasanth.Rajendran", "email": "[email protected]" }]; var object2 = [{ "id": 2, "name": "Raju", "username": "Raju.Rajendran", "email": "[email protected]" }]; var newObj = object1.concat(object2); alert(newObj); </script Commented Apr 25, 2015 at 8:22

3 Answers 3

3

object1 and object2 are arrays. You can use the concat method to concatenate arrays.

newObj = object1.concat(object2);

var object1 = [{
    "id": 11,
    "name": "Vasanth",
    "username": "Vasanth.Rajendran",
    "email": "[email protected]",
    "address": {
      "street": "Nungampakkam",
      "suite": "No154",
      "city": "Chennai",
      "zipcode": "31428-2261",
      "geo": {
        "lat": "-38.2386",
        "lng": "57.2232"
      }
    },
    "phone": "024-648-3804",
    "website": "google.net",
    "company": {
      "name": "Test",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }];

var object2 = [{
    "id": 2,
    "name": "Raju",
    "username": "Raju.Rajendran",
    "email": "[email protected]",
    "address": {
      "street": "Nungampakkam",
      "suite": "No154",
      "city": "Chennai",
      "zipcode": "31428-2261",
      "geo": {
        "lat": "-38.2386",
        "lng": "57.2232"
      }
    },
    "phone": "024-648-3804",
    "website": "google.net",
    "company": {
      "name": "Test",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }];

var newObject = object1.concat(object2);
console.log(newObject);

Sign up to request clarification or add additional context in comments.

4 Comments

Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. <script> var object1 = [{ "id": 1, "name": "Vasanth", "username": "Vasanth.Rajendran", "email": "[email protected]" }]; var object2 = [{ "id": 2, "name": "Raju", "username": "Raju.Rajendran", "email": "[email protected]" }]; var newObj = object1.concat(object2); alert(newObj); </script
That's what you should get. You started with two arrays that each have one object in them. The result is one array with two objects in it.
alert() won't show the contents of an object. Use console.log().
Or alert(JSON.stringify(newObj)).
2

Try this:

var newObj = [object1[0], object2[0]];

OR

var newObj = object1.concat(object2);

concat creates a new array consisting of the elements in the object on which it is called, followed in order by, for each argument, the elements of that argument (if the argument is an array) or the argument itself (if the argument is not an array).

Reference: Array.prototype.concat()

2 Comments

Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. <script> var object1 = [{ "id": 1, "name": "Vasanth", "username": "Vasanth.Rajendran", "email": "[email protected]" }]; var object2 = [{ "id": 2, "name": "Raju", "username": "Raju.Rajendran", "email": "[email protected]" }]; var newObj = object1.concat(object2); alert(newObj); </script
Use console.log(newObj); instead of alert, you will get the object you wanted
0

They are arrays as I can see. You can do:

var object3 = object1.concat(object2);

    //you can access your objects like this: object3[0] and object3[1]
    //Or in for loop
   for (i=0; i<object3.length; i++) {
       console.log(object3[i]);
   }

Otherwise for objects you can check How can I merge properties of two JavaScript objects dynamically?

For reference:

var array = [] // this is array
var theObject = {} // json object

if you want to merge them into one object try:

jQuery.extend(object1[0], object2[2]);

But if you do like this all your properties will be replaced, because they are the same. That is why the above method is best for your case

3 Comments

Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. <script> var object1 = [{ "id": 1, "name": "Vasanth", "username": "Vasanth.Rajendran", "email": "[email protected]" }]; var object2 = [{ "id": 2, "name": "Raju", "username": "Raju.Rajendran", "email": "[email protected]" }]; var newObj = object1.concat(object2); alert(newObj); </script
use console.log(newObj); It's an array of two objects. You can access them newObj[0] and newObj[1] @rrvasanth
check out edits, I gave you maximum infromation I could @rrvasanth

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.