3

So what i'm trying to achieve is creating a json flat file to store user info where i'm stuck is that i don't know how to add new nested objects that are empty and then save the json file and reload it.

what i have in my *json* file is this.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        }
    }
}

I'm trying to add as many new objects as i want to it. So for example.

{
    "users" : {
        "test" : {

        },
        "test1" : {

        },
        "test2" : {

        },
        "test3" : {

        }
    }
}

My server side Javascript

    json.users.push = username;
    fs.writeFile("./storage.json", JSON.stringify(json, null, 4) , 'utf-8');
    delete require.cache[require.resolve('./storage.json')];
    json = require("./storage.json");

With this code it does not write the file so when the require is done i end up with the same file and my json object ends up like this when i console.log it

{                                                                                                                                                    
    "users": {                                                                                                                                       
        "test": {},                                                                                                                                  
        "test1": {},                                                                                                                                 
        "push": "test2"                                                                                                                                
    }                                                                                                                                                
}

Please do not recommend some external module to solve something has simple as this. Also if any one can point me to a in depth json documentation that gets straight to the point with what i'm try to do it would be appreciated

2
  • 2
    there is no such thing as JSON object. there's object, and there's JSON. it's the difference between a cake-recipe and a cake. there is no such thing cake-recipe-cake. Commented Oct 5, 2015 at 3:12
  • What do you think json.users.push does? Commented Oct 5, 2015 at 4:03

1 Answer 1

3

Use [] to access a dynamic key on the object

json.users[username] = {a: 1, b: 2}

Be careful naming your variable like that tho because json the way you're using it is not JSON. JSON is a string, not an object with keys.

See the below demo for distinction

var json = '{"users":{"test1":{},"test2":{}}}';
var obj  = JSON.parse(json);
var newuser = 'test3';
obj.users[newuser] = {};
console.log(JSON.stringify(obj));
//=> {"users":{"test1":{},"test2":{},"test3":{}}}

Sign up to request clarification or add additional context in comments.

5 Comments

Straight to the point! You're awesome! Do you know where i can find a in depth documentation on manipulating json with JavaScript also do you have any clue why my fs.writefile is not working?
You never manipulate JSON directly. Always var x = JSON.parse(yourJson), perform manipulations on x, then JSON.stringify(x) to rewrite the JSON.
I understand that what i am doing is non standard and that i must watch myself carefully to not screw up.
it looks like you're trying to use fs.writeFile in a synchronous way, maybe try fs.writeFileSync. If that doesn't help, you should ask a new question.
no i know that you use parse and stringify. i was not using manipulation in that sense.

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.