mergeJSON.js (829B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 3 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 /* -*- indent-tabs-mode: nil; js-indent-level: 4 -*- */ 6 7 var infiles = [...scriptArgs]; 8 var outfile = infiles.pop(); 9 10 let output; 11 for (const filename of infiles) { 12 const data = JSON.parse(os.file.readFile(filename)); 13 if (!output) { 14 output = data; 15 } else if (Array.isArray(data) != Array.isArray(output)) { 16 throw new Error('mismatched types'); 17 } else if (Array.isArray(output)) { 18 output.push(...data); 19 } else { 20 Object.assign(output, data); 21 } 22 } 23 24 var origOut = os.file.redirect(outfile); 25 print(JSON.stringify(output, null, 4)); 26 os.file.close(os.file.redirect(origOut));