In this tutorial, we will use an example to introduce steps to merge json objects in javascript.
1.Create two json objects
const string1 = '{ "name": "Todd", "age": 20 }'; const string2 = '{ "languages": ["Spanish", "Portuguese"], "married": true }';
2.Parse json object in javascript
const obj1 = JSON.parse(string1); const obj2 = JSON.parse(string2);
To understand how to parse json in javascript. Here is a tutorial:
JavaScript: Parse JSON – A Beginner Guide
3.Start to merge two json objects
const mergedObject = { obj1, obj2 };
4.Display merged json object
console.log(JSON.stringify(mergedObject))
Run this code, you may see: