1

I have a problem to inserting new json into old json, have been tried to find it but I don't know the right keyword,

let say I have a json on foo1.js:

function jsonapi(){
  var ItemJSON = {
    "salutation":"foo1"
    ,"location":"foo2"
    ,"reference_code":"foo3"
  }
}

and on foo2.js :

jsonapi();
var json = {
  ,"isCustomer":"foo4"
  ,"email_to":"foo5"
  ,"phone":"foo6"
}

Goal :

{
  "salutation":"foo1"
  ,"location":"foo2"
  ,"reference_code":"foo3"
  ,"isCustomer":"foo4"
  ,"email_to":"foo5"
  ,"phone":"foo6"
}

there's a way to archieve my goal?

flix
  • 1,688
  • 3
  • 34
  • 64
  • That's not JSON. Is it possible to "merge JavaScript objects"? Yes. Of course, having *access* to both objects at the same time is pretty much a prerequisite (ie. return the first object, pass in the second object, use a global variable, or some other combination).. Then it's just `f(obj1, obj2)` where `f` is the function that "merges" said objects; again, both `obj1` and `obj2` (neither of which are JSON in the shown code) are *available* at the same time. – user2864740 Jan 22 '18 at 04:31
  • no, you can't push a string into a string (easily) – Jaromanda X Jan 22 '18 at 04:32
  • @JaromandaX There are no JSON strings in the question. – user2864740 Jan 22 '18 at 04:32
  • Checkout `Object.assign`: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign – Mark Jan 22 '18 at 04:33
  • @user2864740 - I know that - but the title and question keep repeating `JSON` – Jaromanda X Jan 22 '18 at 04:35
  • So call it out: "Hey, that's not JSON! Those are two JavaScript objects. Merging JSON is hard because it's a string, but merging two JavaScript objects (that you have shown in the post) is easy because they aren't strings!" That is a big difference from saying "Strings [which you don't have because that is not JSON] are hard to merge." – user2864740 Jan 22 '18 at 04:35
  • @user2864740 may you give me an example code how to merge 2 JSObject? – flix Jan 22 '18 at 04:50
  • @flix see my answer – zer00ne Jan 22 '18 at 05:11

4 Answers4

1

There is a reasonably comprehensive answer here: How to join two JavaScript Objects, without using JQUERY

However, stealing the first answer from the above link:

var result = {};
for(var key in obj1) result[key] = obj1[key];
for(var key in obj2) result[key] = obj2[key];
Doug
  • 547
  • 10
  • 23
1

The problem is declaration of variable inside the function.

Try as follows.

Declare variable as global and update it in function.

foo1.js

var itemJSON = {};

function jsonapi(){
   itemJSON = {
     "salutation":"foo1",
     "location":"foo2",
     "reference_code":"foo3"
   }
}

foo2.js

jsonapi();
var json = {
  "isCustomer":"foo4",
  "email_to":"foo5",
  "phone":"foo6"
};

Use Object.assign to combine two objects.

let output = Object.assign(itemJSON, json);
console.log(output);

Another way:

Return data from function in foo1.js.

foo1.js

function jsonapi(){
   return {
     "salutation":"foo1",
     "location":"foo2",
     "reference_code":"foo3"
   }
}

foo2.js

var itemJSON = jsonapi();
var json = {
  "isCustomer":"foo4",
  "email_to":"foo5",
  "phone":"foo6"
};

let output = Object.assign(itemJSON, json);
console.log(output);
Harish Kommuri
  • 2,825
  • 1
  • 22
  • 28
0

Here's an example using Javascript .concat method.

More @ Array.prototype.concat()

var array1 = [
{
"name":"salutation",
"value":"foo1"
},
{
"name":"location",
"value":"foo2"
},
{
"name":"reference_code",
"value":"foo3"
}
];

var array2 = [
{
"name":"isCustomer",
"value":"foo4"
},
{
"name":"email_to",
"value":"foo5"
},
{
"name":"phone",
"value":"foo6"
}
];

console.log(array1.concat(array2));
suchislife
  • 4,251
  • 10
  • 47
  • 78
0

You use jsonapi as function ,so if you want to get that func value you need to return data !!! Take a look down at how Object.assign work ....

The Object.assign() method is used to copy the values of all enumerable own properties from one or more source objects to a target object. It will return the target object.

function jsonapi(){
   return {
    "salutation":"foo1","location":"foo2","reference_code":"foo3"
  };
}

var ItemJSON = jsonapi();

var json = {
  "isCustomer":"foo4"
  ,"email_to":"foo5"
  ,"phone":"foo6"
}

var result = Object.assign({},ItemJSON, json);
console.log(result);
Jack jdeoel
  • 4,554
  • 5
  • 26
  • 52