1

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

var object1 = [{
    "id": 11,
    "name": "Vasanth",
    "username": "Vasanth.Rajendran",
    "email": "vasanth@mail.com",
    "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": "Raju@mail.com",
    "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": "Sincere@april.biz",
    "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": "Shanna@melissa.tv",
    "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"
    }
  }];
Barmar
  • 741,623
  • 53
  • 500
  • 612
rrvasanth
  • 2,831
  • 3
  • 15
  • 9
  • 1
    Why are the names different from the input to the output? – Barmar Apr 25 '15 at 07:55
  • Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. – rrvasanth Apr 25 '15 at 08:22

3 Answers3

4

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": "vasanth@mail.com",
    "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": "Raju@mail.com",
    "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);
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. – rrvasanth Apr 25 '15 at 08:14
  • 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. – Barmar Apr 25 '15 at 08:27
  • `alert()` won't show the contents of an object. Use `console.log()`. – Barmar Apr 25 '15 at 08:27
  • Or `alert(JSON.stringify(newObj))`. – Barmar Apr 25 '15 at 08:28
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()

Satpal
  • 132,252
  • 13
  • 159
  • 168
Tushar
  • 85,780
  • 21
  • 159
  • 179
  • Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. – rrvasanth Apr 25 '15 at 08:17
  • Use `console.log(newObj);` instead of `alert`, you will get the object you wanted – Tushar Apr 25 '15 at 08:20
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

Community
  • 1
  • 1
Said Kholov
  • 2,436
  • 1
  • 15
  • 22
  • Hi Thank you for you reply. I tried like below script am getting [object, Object],[object, Object] message. – rrvasanth Apr 25 '15 at 08:28
  • use `console.log(newObj);` It's an array of two objects. You can access them newObj[0] and newObj[1] @rrvasanth – Said Kholov Apr 25 '15 at 08:37
  • check out edits, I gave you maximum infromation I could @rrvasanth – Said Kholov Apr 25 '15 at 08:42