How to insert into JSON?

Now that I can write my own answer, try this instead:

var obj = {
        index: {
            colors: []
        }
    }, // basic object
    single = {
        title: "great color",
        name: "nice color"
    };

obj.index.colors.push(single);

// ...

fs.writeFile(file, JSON.stringify(obj, null, 2));

I’m not sure why you need to make the objects separately but I just assumed you were adding the indeces for the colors somewhere else so I left that separate. It’s just less verbose this way.

Leave a Comment