Unexpected token _ at Object.parse (native)

31 views
Skip to first unread message

abhinanda...@gmail.com

unread,
Oct 6, 2018, 3:55:06 PM10/6/18
to nodejs
we have a StringObject  

var data= "[{ _id: '5871547e5646511100195e9b',date: '2018-10-05T09:57:39.230Z' }]"

and i want to parse over string object into JSON Object

JSON.parse(data)


then i am getting

SyntaxError: Unexpected token _

    at Object.parse (native)


Please help

Akuma Isaac

unread,
Oct 6, 2018, 4:46:32 PM10/6/18
to nod...@googlegroups.com
The json is not a valid json 
The keys have be quote with double quote.
Try to read about json structure

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/cbbcb9c6-450f-447b-848d-3054f498ba14%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

kai zhu

unread,
Oct 6, 2018, 4:59:57 PM10/6/18
to nod...@googlegroups.com
hi abhinandanyadav,  strings inside json must be double-quoted (not single-quote or none).  the following code will work:

```javascript
// valid JSON
JSON.parse('[{"_id":"5871547e5646511100195e9b","date":"2018-10-05T09:57:39.230Z"}]')
```

Zlatko

unread,
Oct 8, 2018, 9:41:17 PM10/8/18
to nodejs
Hello,

In addition to what the others suggested, I'll suggest removing the outer, double quotes around your thing and it's going to be an object instantly, no de-serialization needed. That depends on your use case, though, but in the example you provided, it will work's
Zlatko

Reply all
Reply to author
Forward
0 new messages