balaphp
balaphp

Reputation: 1326

Node.js parse json

i am trying to convert the string to JSON. This is my string

{_id : demo,version : 2,members : [{_id : 0,host : 192.168.1.46:27017},{_id : 1,host : 192.168.1.93:27017}]}

i am trying to convert the string to json but its showing error

 SyntaxError: Unexpected token _

I have tried all json functions to convert this to JSON but its getting failed because of the single quotes are missing in the string...

How to fix this ?

Upvotes: 0

Views: 139

Answers (2)

CFrei
CFrei

Reputation: 3627

If you can modify your string a bit and add "" around the string values, you can use the function eval() to get it parsed.

a='{_id : "demo",version : 2,members : [{_id : 0,host : "192.168.1.46:27017"},{_id : 1,host : "192.168.1.93:27017"}]}'
v=eval('('+a+')')

This is not really recommended - if someone gives you a js function-call instead of this string, you would execute it without knowing...

Upvotes: 0

fiskeben
fiskeben

Reputation: 3467

The keys in your JSON document should be in quotes: {"_id": "value"}

Upvotes: 1

Related Questions