I think this could be a possible solution:

const fs = require("fs");

let files = fs.readdirSync("library");

function mergeJson() {
  const output = files.reduce((acum, current) => {
    data = fs.readFileSync(`./library/${current}`);
    const newData = JSON.parse(data);

    return {
      ...acum,
      ...newData,
    };
  }, {});

  if (output) {
    fs.writeFileSync(`combine.json`, JSON.stringify(output), { flag: "a" });
  }
}

mergeJson();
Answer from Franco Aguilera on Stack Overflow
🌐
Medium
ekapramudita.medium.com › combining-multiple-json-files-into-a-single-json-file-8d6e608483e0
Combining Multiple JSON Files Into A Single JSON File | by Eka Aditya Pramudita | Medium
June 7, 2021 - It is based on a subset of JavaScript language (the way objects are built-in JavaScript). JSON is often used when data is sent from a server to a web page so that JSON data processing in data analytics becomes inevitable. In this article, I will show you an example of JSON data processing. The goal is to make JSON files containing one observation per file in a folder combined into a single JSON file.
🌐
Quora
quora.com › How-can-I-load-more-JSON-files-into-one-JSON-file-and-then-read-it-in-console-JavaScript
How to load more JSON files into one JSON file, and then read it in console (JavaScript) - Quora
... A simpler way to do it is to use the spread operator. ... GitHub - jhildenbiddle/mergician: Uniquely flexible and light-weight utility for cloning and deep (recursive) merging of JavaScript objects.
🌐
npm
npmjs.com › package › json-merger
json-merger - npm
Merge JSON (or YAML) files and objects with operations like $import $remove $replace $merge and more.
      » npm install json-merger
    
Published   Feb 02, 2025
Version   3.0.0
Author   Niek Bosch
🌐
GitHub
github.com › mtacnet › json-merger
GitHub - mtacnet/json-merger: Merge multiple JSON files - Vanilla JavaScript and HTML (graphic mode: browser+explorer)
Merge multiple JSON files - Vanilla JavaScript and HTML (graphic mode: browser+explorer) - mtacnet/json-merger
Starred by 4 users
Forked by 4 users
Languages   JavaScript 89.1% | HTML 10.9% | JavaScript 89.1% | HTML 10.9%
🌐
GitHub
github.com › userpixel › combine-json
GitHub - alexewerlof/combine-json: Combines a group of files into one JavaScript object
June 28, 2021 - const { combine } = require('combine-json') const myBigJsonObject = await combine('path/to/the/root/dir')
Starred by 17 users
Forked by 2 users
Languages   JavaScript 100.0% | JavaScript 100.0%
🌐
npm
npmjs.com › package › gulp-merge-json
gulp-merge-json - npm
var merge = require('gulp-merge-json'); /** * Basic functionality */ gulp.src('jsonFiles/**/*.json') .pipe(merge()) .pipe(gulp.dest('./dist')); /** * Edit JSON with function */ gulp.src('jsonFiles/**/*.json') .pipe(merge({ fileName: 'file.json', edit: (parsedJson, file) => { if (parsedJson.someValue) { delete parsedJson.otherValue; } return parsedJson; }, })) .pipe(gulp.dest('./dist')); /** * Edit final JSON with transformer function */ gulp.src('jsonFiles/**/*.json') .pipe(merge({ fileName: 'file.json', transform: (mergedJson) => { return { key: { type: 'data', ...mergedJson, }; }; }, })) .pi
      » npm install gulp-merge-json
    
Published   Feb 29, 2024
Version   2.2.1
Author   Josh Swan
Find elsewhere
🌐
techtutorialsx
techtutorialsx.wordpress.com › 2020 › 09 › 06 › javascript-merge-json-objects
JavaScript merge JSON objects – techtutorialsx
January 25, 2025 - Additionally, we should consider that if we had the same property name in both original objects, the value of the property of the last object being spreaded would be the one kept in the merged object. After this, we will convert the merged object back to a JSON string with a call to the stringify method on the JSON object. You can check here a more detailed tutorial on how to serialize a JavaScript object to JSON.
🌐
Quora
quora.com › How-do-I-combine-different-JSON-files-in-JavaScript-urgent
How to combine different JSON files in JavaScript urgent - Quora
Answer (1 of 2): Can you please elaborate more or drop the question on Stack Overflow - Where Developers Learn, Share, & Build Careers.
🌐
Quora
quora.com › How-can-I-add-two-JSON-objects-into-one-object-JavaScript
How to add two JSON objects into one object JavaScript - Quora
Answer (1 of 4): The Spread syntax could be very helpful here, depending upon exactly what you want to do. [code]let obj1 = { a : ‘aValue’, b : ‘bValue’ }; let obj2 = {c : ‘cValue’, d : ‘dValue’, a : 'obj2AValue' }; let combined = { …obj1, …obj2 }; [/code]Note that if there ...
🌐
GitHub
github.com › boschni › json-merger
GitHub - boschni/json-merger: Merge JSON files and objects with indicators like $import $remove $replace $merge
Use $move.value to not only move the item but also merge it with a value. ... { "someArray": [ { "$match": { "query": "$[?(@.a == 3)]", "value": { "$move": { "index": 0, "value": { "b": 3 } } } } } ] } ... Use the $select operation to select ...
Starred by 50 users
Forked by 17 users
Languages   JavaScript 62.7% | TypeScript 37.3% | JavaScript 62.7% | TypeScript 37.3%
🌐
DevGenius
blog.devgenius.io › combining-json-objects-in-javascript-node-js-abe84f492d0
Combining JSON Objects in JavaScript/Node.js | by Popa Vlad | Dev Genius
March 6, 2025 - This method copies the values of ... const jsonObj2 = { city: "New York", hobby: "Coding" }; // Merge JSON objects const mergedJSON = Object.assign({}, jsonObj1, jsonObj2); console.log(mergedJSON);...
🌐
Quora
quora.com › How-can-you-add-two-JSON-objects-together-and-convert-them-to-one-JSON-object-without-losing-any-information
How to add two JSON objects together and convert them to one JSON object without losing any information - Quora
If your goal is to combine two objects into one before turning them into a JSON string you can use Object.assign. If your two objects contain any of the same properties then the second argument of Object.assign will overwrite the first.
🌐
Stack Overflow
stackoverflow.com › questions › 62990490 › javascript-read-process-and-combine-json-from-multiple-files
JavaScript Read, Process and Combine JSON from Multiple Files - Stack Overflow
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/2.0.2/FileSaver.min.js"></script> <script type="text/javascript"> var JSONData = ""; function encodeJSON(file, t) { JSONData += 'var ' + file.name.split('.')[0] + ' = ' + t.result + '\n'; } function processFiles() { var filelist = document.getElementById('files').files; var reader = new FileReader(); reader.onload = function(e) { for(let i=0; i<filelist.length; i++) { encodeJSON(filelist[i], e.target) } var blob = new Blob([JSONData], {type: "text/plain;charset=utf-8"}); saveAs(blob, 'encoded.js'); } reader.readAsText(filelist[0], 'UTF-8') } </script> </head> <body> <p>JSON inputs:</p> <p><input type="file" multiple id="files" onchange="processFiles()" /></p> </body> </html>
🌐
YouTube
youtube.com › watch
How to Merge Multiple JSON Files in JavaScript - YouTube
Learn how to programmatically merge multiple JSON files into one correctly formatted JSON file using JavaScript. Step-by-step instructions included!---This v...
Published   April 4, 2025
Views   18
Top answer
1 of 10
123

You want the concat method.

var finalObj = json1.concat(json2);
2 of 10
39

Upon first appearance, the word "merg" leads one to think you need to use .extend, which is the proper jQuery way to "merge" JSON objects. However, $.extend(true, {}, json1, json2); will cause all values sharing the same key name to be overridden by the latest supplied in the params. As review of your question shows, this is undesired.

What you seek is a simple javascript function known as .concat. Which would work like:

var finalObj = json1.concat(json2);

While this is not a native jQuery function, you could easily add it to the jQuery library for simple future use as follows:

;(function($) {
    if (!$.concat) {
        $.extend({
            concat: function() {
                return Array.prototype.concat.apply([], arguments);
            }
        });
    }
})(jQuery);

And then recall it as desired like:

var finalObj = $.concat(json1, json2);

You can also use it for multiple array objects of this type with a like:

var finalObj = $.concat(json1, json2, json3, json4, json5, ....);

And if you really want it jQuery style and very short and sweet (aka minified)

;(function(a){a.concat||a.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

;(function($){$.concat||$.extend({concat:function(){return Array.prototype.concat.apply([],arguments);}})})(jQuery);

$(function() {
    var json1 = [{id:1, name: 'xxx'}],
        json2 = [{id:2, name: 'xyz'}],
        json3 = [{id:3, name: 'xyy'}],
        json4 = [{id:4, name: 'xzy'}],
        json5 = [{id:5, name: 'zxy'}];
    
    console.log(Array(10).join('-')+'(json1, json2, json3)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3));
    console.log(Array(10).join('-')+'(json1, json2, json3, json4, json5)'+Array(10).join('-'));
    console.log($.concat(json1, json2, json3, json4, json5));
    console.log(Array(10).join('-')+'(json4, json1, json2, json5)'+Array(10).join('-'));
    console.log($.concat(json4, json1, json2, json5));
});
center { padding: 3em; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<center>See Console Log</center>

jsFiddle

🌐
n8n
community.n8n.io › questions
Combine multiple json files into one json file - Questions - n8n Community
June 15, 2022 - Describe the issue/error/question I would like to read multiple json file from the local disk and then combine all the json data from all the files into one main json data file. What is the error message (if any)? I am not getting any error but I have been trying to use read binary files and ...