5

How to join two JSON Array objects in Node.

I want to join obj1 + obj2 so I can get the new JSON object:

obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]


output = [ { t: 1, d: 'AAA', v: 'yes' },
           { t: 2, d: 'BBB', v: 'yes' },
           { t: 3, d: 'CCC', v: 'yes' },
           { t: 4, d: 'DDD', v: 'yes' }]
Ratan Uday Kumar
  • 5,738
  • 6
  • 35
  • 54
coriano35
  • 165
  • 1
  • 3
  • 10
  • Array.prototype.concat ? – Taplar Dec 20 '16 at 03:25
  • 1
    http://stackoverflow.com/questions/14974864/combine-or-merge-json-on-node-js-without-jquery – User Dec 20 '16 at 03:52
  • Does this answer your question? [Merge two json/javascript arrays in to one array](https://stackoverflow.com/questions/10384845/merge-two-json-javascript-arrays-in-to-one-array) – Vega Jun 10 '20 at 16:04

7 Answers7

10

var output = obj1.concat(obj2);

therobinkim
  • 2,500
  • 14
  • 20
  • @coriano35 You need to provide more information. What does "not working" mean? Are you given an error? Or does your `var output` look nothing like what you expected? – therobinkim Dec 20 '16 at 03:30
  • @coriano35 Great, we're a step closer! What is `obj1`? Where is it defined? Feel free to edit your original question to tell us how `obj1` really gets its value. – therobinkim Dec 20 '16 at 03:40
  • the obj1 value come from a url data with a format like this: ,,,,, 19840105,10.5435,10.6522,10.5435,10.6522,2300 19840106,10.6522,10.7609,10.6522,10.7609,36572 19840109,10.7609,10.7609,10.7609,10.7609,2992 19840110,10.8696,10.9783,10.8696,10.9783,127648 19840111,11.0869,11.0869,11.0869,11.0869,26912 19840112,11.0869,11.4131,11.0869,11.4131,21620 – coriano35 Dec 20 '16 at 03:49
  • and parse by a function: var result = { d: [], t: [], c: [], v: [] }; var lines = data.split('\n'); for (var i = 1; i < lines.length - 1; i++) { var items = lines[i].split(","); result.d.push(items[0]); result.t.push(items[1]); result.c.push(parseFloat(items[2])); result.v.push(parseFloat(items[3])); } – coriano35 Dec 20 '16 at 03:49
  • and put it in a json object – coriano35 Dec 20 '16 at 03:51
  • @coriano35 Please add this information into the question so that it's easier to read. I will not be able to provide additional assistance otherwise! – therobinkim Dec 20 '16 at 03:51
8
obj1 = [ { t: 1, d: 'AAA', v: 'yes' },
         { t: 2, d: 'BBB', v: 'yes' }]

obj2 = [ { t: 3, d: 'CCC', v: 'yes' },
        { t: 4, d: 'DDD', v: 'yes' }]

var output = obj1.concat(obj2);

console.log(output);
Vaghani Janak
  • 601
  • 5
  • 14
6

try

  Object.assign(obj1, obj2);

For Details check Here

 var o1 = { a: 1 };
 var o2 = { b: 2 };
 var o3 = { c: 3 };

 var obj = Object.assign(o1, o2, o3);
 console.log(obj); // { a: 1, b: 2, c: 3 }
User
  • 1,334
  • 5
  • 29
  • 61
2

It can be done easily using ES6,

const output = [...obj1, ...obj2];
Evert
  • 93,428
  • 18
  • 118
  • 189
Amit Verma
  • 21
  • 2
0

i already got an answer from the link provided by Pravin

var merge = function() {
var destination = {},
    sources = [].slice.call( arguments, 0 );
sources.forEach(function( source ) {
    var prop;
    for ( prop in source ) {
        if ( prop in destination && Array.isArray( destination[ prop ] ) ) {

            // Concat Arrays
            destination[ prop ] = destination[ prop ].concat( source[ prop ] );

        } else if ( prop in destination && typeof destination[ prop ] === "object" ) {

            // Merge Objects
            destination[ prop ] = merge( destination[ prop ], source[ prop ] );

        } else {

            // Set new values
            destination[ prop ] = source[ prop ];

        }
    }
});
return destination;
};

console.log(JSON.stringify(merge({ a: { b: 1, c: 2 } }, { a: { b: 3, d: 4 } })));
coDe murDerer
  • 1,858
  • 4
  • 20
  • 28
coriano35
  • 165
  • 1
  • 3
  • 10
-1

you can use jmerge package.

npm i jmerge
const jm = require('jmerge')

jm(obj1,obj2,obj3,...) //merging json data
Tegar Santosa
  • 57
  • 1
  • 7
-2

I simply convert the arrays to strings, join them crudely with a comma, and then parse the result to JSON:

newJson=JSON.parse(
    JSON.stringify(copyJsonObj).substring(0,JSON.stringify(copyJsonObj).length-1) +
    ',' + 
    JSON.stringify(jsonObj).substring(1)
)
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83