1

I have sent a post request to node express server

    app.post('/addUsers', function (req, res) {

   var newuser =  req.body;
   console.log(newuser);
   fs.readFile( __dirname + "/" + "users.json", 'utf8', function (err, data) {
     data = JSON.parse( data );
     data["user4"] = newuser // how can i create  a user4 here currently i get an error as newsier is whole req body or something .
     console.log( data );
//       res.end(data);
       res.end( JSON.stringify(data));
   });
})

and i have got the value of newuser to console as

{ '{"user4":{"password":"killer","id":4,"profession":"kuchbhi","name":"mukesh"}}': '' }

The question is how can i get the value of user4 so that i can add it to the list of users .

I was using this dictionary in swift

                                   ["user4":["name" : "mukesh",
                                     "password" : "killer",
                                     "profession" : "kuchbhi",
                                     "id": 4]]

which i changed to this once knowing its not valid json

                                ["name" : "mukesh",
                                 "password" : "killer",
                                 "profession" : "kuchbhi",
                                 "id": 4]

and know i don't get the error but the data from server response (i have printed it in Xcode console)after adding the fourth user is quite not in the format i think it should be , it looks like this

["user2": {
    id = 2;
    name = suresh;
    password = password2;
    profession = librarian;
}, "user4": {
    "{\"password\":\"killer\",\"profession\":\"kuchbhi\",\"id\":4,\"name\":\"mukesh\"}" = "";
}, "user1": {
    id = 1;
    name = mahesh;
    password = password1;
    profession = teacher;
}, "user3": {
    id = 3;
    name = ramesh;
    password = password3;
    profession = clerk;
}]

all other entries are added by me manually in users.son. This is what is logged into terminal console

  user4: { '{"password":"killer","profession":"kuchbhi","id":4,"name":"mukesh"}': '' } } 

which i still think is not good json or is it ?

Thank you quite a novice with format conversions and servers get/post thingy .

Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Sourav Sachdeva
  • 353
  • 6
  • 20
  • Your Json object hast only one field called: '{"user4":{"password":"killer","id":4,"profession":"kuchbhi","name":"mukesh"}}' with the value: '' Is this on purpose? – Nick D Apr 07 '16 at 11:11
  • This is not a valid json. Please make sure first you have a valid json. – Gurpinder Apr 07 '16 at 11:23

1 Answers1

0

At the moment, the json that you receive/present is not valid.

Once you actually manage to receive valid JSON from the client, this will look like the following.

var u = {"user4":{"password":"killer","id":4,"profession":"kuchbhi","name":"mukesh"}};

For any json you can get its keys by using the command

var theKeys = Object.keys(u);

In the example above, there is only one key for the entire JSON, so the variable theKeys has contents [ 'user4' ]. Hence, you will be able to access the username that you want by looking at theKeys[0].

Similarly, you can get the properties for the inner json document using the command

Object.keys(u[theKeys[0]]);

which will return [ 'password', 'id', 'profession', 'name' ].

EDIT

Based on the comment below, perhaps the following code can help you send the json object correctly from the client side.

NSDictionary *aDictionary = @{
    @"user4": @{@"password": @"killer",
                @"id": @4,
                @"profession": @"kuchbhi"
              }};

NSError * error;
NSData * theData = [NSJSONSerialization dataWithJSONObject:aDictionary options:0 error:&error];
NSString * newStr = [[NSString alloc] initWithData:theData encoding:NSUTF8StringEncoding];

NSLog(@"body: %@", newStr);
[newStr release];

And here is a solution to send json correctly using swift: https://stackoverflow.com/a/31938246/1355058

Community
  • 1
  • 1
MightyMouse
  • 13,208
  • 8
  • 33
  • 43
  • I am sending NSData to httpBody ,so should i take nsdata at server and convert it to valid json their or there must be a way to send {"user4":{"password":"killer","id":4,"profession":"kuchbhi","name":"mukesh"}} this json in httpbody .so i can use it as such . Can u guide me at this. – Sourav Sachdeva Apr 07 '16 at 13:53
  • You should send a valid json from the client side and then it will look like the one above. Most likely NSJSONSerialization is an easy solution for your client: http://stackoverflow.com/a/9885928/1355058 – MightyMouse Apr 07 '16 at 13:55
  • I updated the post above to give you an example of the string that you can post on the body of your http request. Also, since you are using swift, perhaps this one is better suited for you http://stackoverflow.com/a/31938246/1355058 so that you can send a json object on the body of the http request using swift. – MightyMouse Apr 07 '16 at 14:21
  • You are welcome. Do not forget to accept the answer then. – MightyMouse Apr 07 '16 at 14:33