admin管理员组

文章数量:1327665

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",
    "pany": {
      "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",
    "pany": {
      "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",
    "pany": {
      "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",
    "pany": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  }];

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",
    "pany": {
      "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",
    "pany": {
      "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",
    "pany": {
      "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",
    "pany": {
      "name": "Deckow-Crist",
      "catchPhrase": "Proactive didactic contingency",
      "bs": "synergize scalable supply-chains"
    }
  }];
Share Improve this question edited Apr 25, 2015 at 7:54 Barmar 783k56 gold badges547 silver badges660 bronze badges asked Apr 25, 2015 at 7:53 rrvasanthrrvasanth 2,9213 gold badges16 silver badges9 bronze badges 2
  • 1 Why are the names different from the input to the output? – Barmar 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 – rrvasanth Commented Apr 25, 2015 at 8:22
Add a ment  | 

3 Answers 3

Reset to default 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",
    "pany": {
      "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",
    "pany": {
      "name": "Test",
      "catchPhrase": "Centralized empowering task-force",
      "bs": "target end-to-end models"
    }
  }];

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

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()

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

本文标签: How to merge two JSON data in javascriptJQUERYStack Overflow