How to add a JSON object as a new level to another JSON object?

I have code that eventually collects two JSON objects, something like this.

var jsonL1 = {"holder1": {}}
var jsonL2 = {"section":"0 6","date":"11/12/13"}

I would like to insert jsonL2 inside jsonL1.holder1 and merge it into a single JSON object.

Required conclusion

{
    "holder1": {
        "section": "0 6",
        "date": "11/12/13"
    }
}

How can i do this?

+3
source share
2 answers

It is as simple as:

L1.holder1 = L2

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

See also: No JSON Object

You can also learn more about objects in JavaScript .

+3
source

If you want the first object to refer to the second, do the following:

jsonL1.holder1 = jsonL2;

, .

, , , . , jsonL2 jsonL1.holder, .


, JSON javascript:

    // this is a javascript object
var obj = {"section":"0 6","date":"11/12/13"};

    // this is valid JSON data
var jsn = '{"section":"0 6","date":"11/12/13"}';
+3

Source: https://habr.com/ru/post/1788011/


All Articles