1

I am sending the string from AngularJS to NodeJS in following form.

"{↵obj:{↵one:string,↵two:integer↵}↵}"//request object from browser console

I want to convert this string in object and use its properties.for this the server side code is following:

var data=req.body.data.replace(/"/g,"");
var str=data.replace(/\n/g," ");
//res.json(str) // returning "{ obj:{ one:string, two:integer } }"
try {
var obj=JSON.parse(JSON.stringify(str)).replace(/"/g,"");
res.json(obj);//returning same "{ obj:{ one:string, two:integer } }"
} catch (error) {
console.log(error);
}

I want to get ['users'] by Object.keys[obj] or by any other method want to access the properties of this object.but failed to achieve this.

Many solutions like This couldn't be helpful.any suggestion here??

4
  • 2
    Why are you removing quotes and newlines? If it's valid JSON then all you need to do is call JSON.parse() on req.body.data. Commented Nov 23, 2016 at 7:32
  • It seems that this is not a valid JSON Commented Nov 23, 2016 at 7:37
  • it is not parsing req.body.data directly.by doing this JSON.parse(JSON.stringify(req.body.data)) the answer is "{\nobj:{\none:string,\ntwo:integer\n}\n}".....still properties are not accessible Commented Nov 23, 2016 at 7:39
  • @ThomasThiebaud error after json.parse shows this but how can i validate this json in case.???can u please explain? Commented Nov 23, 2016 at 7:42

2 Answers 2

2

All you need is JSON.parse to convert string to object.

Something like this:

var jsonString='{"obj":{"one":"string","two":"integer"}}';
console.log(JSON.parse(jsonString));

Output:

{ obj: { one: 'string', two: 'integer' } }
Sign up to request clarification or add additional context in comments.

11 Comments

just json.parse is creating error as unexpected token.
Show the console output.
this is req.body.data { obj:{ one:string, two:integer } } not like json object.and this is console output.SyntaxError: Unexpected token o in JSON at position 2 at Object.parse (native)
That's because it's not a proper JSON string. This is json object. A json string would be this : '{"obj":{"one":"string","two":"integer"}}';
okay so is this an object string?? can we convert it into simple object instead of json?
|
0

For example my raw data is like this :

var raw_data = 
    {
        "ok": true,
        "user": {
            "id": "U89MZ4PV2",
            "team_id": "T895HCY8H",
            "name": "hyosoka187",       
        }
    }

then just use JSON.parse(raw_data, true);. So

var real_data = JSON.parse(raw_data, true);
console.log(real_data.user.name);

Result :

hyosoka187

NOTE : Output of JSON.parse(raw_data, true) :

{
    ok: true,
    user: {
        id: 'U89MZ4PV2',
        team_id: 'T895HCY8H',
        name: 'hyosoka187',       
    }
}

Comments

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.