How do I add JSON object as new level to another JSON object? How do I add JSON object as new level to another JSON object? json json

How do I add JSON object as new level to another JSON object?


It is as easy as:

L1.holder1 = L2

I removed the "json" from the variable names, as @patrick already said, you are dealing not with "JSON objects" but with object literals.

See also: There is no such thing as a JSON object

You also might want to learn more about objects in JavaScript.


If you want the first object to reference the second, do this:

jsonL1.holder1 = jsonL2;

If you wanted a copy of the second in the first, that's different.

So it depends on what you mean by merge it into one object. Using the code above, changes to jsonL2 will be visible in jsonL1.holder, because they're really just both referencing the same object.


A little off topic, but to give a more visual description of the difference between JSON data and javascript object:

    // this is a javascript objectvar obj = {"section":"0 6","date":"11/12/13"};    // this is valid JSON datavar jsn = '{"section":"0 6","date":"11/12/13"}';