1

I'd like to add JSON object of a json file into another one. Here is an example :

{
    "HOUSES": {
        "1": {
            "NAME": "House1",
            "PEOPLE": {},
            "ID": 1
        },
        "2": {
            "NAME": "House2",
            "PEOPLE": {},
            "ID": 2
        },
        "3": {
            "NAME": "House3",
            "PEOPLE": {},
            "ID": 3
        }
    }
}

And people object :

{

    "1": {
        "NAME": "People1"
    },
    "2": {
        "NAME": "People2"
    },
    "3": {
        "NAME": "People3"
    }
}

Now I'd like to add the people object into House1.

I tried that :

var extend = require('util')._extend;
var obj1 = house.PEOPLE; //var content : {}
var obj2 = extend(people, obj1); //var people content people object

This code replace all house1 by the people object. How can I add people object in the sublevel PEOPLE of house1?

I tried with object-assign too, and I have the same problem.

Thanks !

1
  • Please post a little more context to the code you posted and some output of the code. Commented Jul 21, 2016 at 7:39

1 Answer 1

2

You can do it by:

HOUSES.1.PEOPLE = PEOPLEOOBJ;

Where PEOPLEOBJ is :

PEOPLEOBJ = {

    "1": {
        "NAME": "People1"
    },
    "2": {
        "NAME": "People2"
    },
    "3": {
        "NAME": "People3"
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This will suerly do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.