Multiple Methods to Merge JSON Objects in Node.js Without jQuery

Dec 03, 2025 · Programming · 10 views · 7.8

Keywords: Node.js | JSON Merge | JavaScript Objects

Abstract: This article explores various techniques for merging JSON objects in Node.js, focusing on native JavaScript methods such as Object.assign(), spread operator, and custom function implementations. It provides a detailed comparison of different approaches in terms of applicability, performance considerations, and compatibility issues, with practical code examples to help developers choose the most suitable merging strategy based on specific needs.

Introduction

Merging multiple JSON objects is a common requirement in Node.js development, especially when handling configuration data or API responses. While jQuery's $.extend() method is widely used in browser environments, introducing jQuery in Node.js can be redundant and may cause compatibility issues. Therefore, this article delves into how to achieve similar functionality in Node.js without relying on external libraries.

Native JavaScript Methods

Modern JavaScript offers several native methods for merging objects, which are also applicable in Node.js. First, Object.assign() is a built-in function that copies enumerable properties from one or more source objects to a target object. For example:

var object1 = {name: "John"};
var object2 = {location: "San Jose"};
var merged = Object.assign({}, object1, object2);
console.log(merged); // Output: {name: "John", location: "San Jose"}

This method performs a shallow merge, meaning that if source objects contain nested objects, they are referenced rather than copied. Starting from Node.js 8.6, the spread operator can also be used for merging:

let merged = { ...object1, ...object2 };

The spread operator provides a more concise syntax, but it is essentially similar to Object.assign(), both performing shallow merges.

Custom Merge Function

For more complex scenarios or when fine-grained control over the merging process is needed, a custom function can be written. Referring to the best answer in the Q&A data, a basic merge function implementation is as follows:

function extend(target) {
    var sources = [].slice.call(arguments, 1);
    sources.forEach(function (source) {
        for (var prop in source) {
            if (source.hasOwnProperty(prop)) {
                target[prop] = source[prop];
            }
        }
    });
    return target;
}

var object3 = extend({}, object1, object2);

This function merges by iterating over the properties of source objects and copying them to the target object. Adding a hasOwnProperty check avoids interference from inherited properties, enhancing code robustness. The advantage of custom functions is that they can be extended based on requirements, such as handling property conflicts or implementing deep merges.

Deep Merge and External Libraries

When merging nested objects is necessary, shallow merge methods may be insufficient. For instance, if objects contain other objects as property values, shallow merge only copies references, which could lead to unintended modifications. In such cases, external libraries like deepmerge or lodash.merge can be considered, as they offer recursive merging functionality. However, for simple use cases, a custom deep merge function can be implemented by recursively calling the extend function described above.

Performance and Compatibility Considerations

When selecting a merge method, performance and compatibility must be considered. Object.assign() and the spread operator perform well in modern Node.js versions but may not be compatible with older versions (e.g., Node.js 0.x). For older environments, Node.js's built-in util._extend can be used, but note that it is deprecated. Custom functions, while flexible, may be less efficient when handling large amounts of data. Therefore, it is recommended to choose an appropriate method based on project requirements and target environment.

Conclusion

There are multiple methods for merging JSON objects in Node.js, ranging from native functions to custom implementations. For simple shallow merges, Object.assign() and the spread operator are efficient choices; for scenarios requiring more control or deep merges, custom functions or external libraries may be more suitable. Developers should evaluate specific needs, such as compatibility, performance, and maintainability, to make the best decision. Through the discussion in this article, it is hoped that readers will gain a deeper understanding of these techniques and apply them flexibly in practical projects.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.