HOW TO PARSE INTO JSON OBJECT USING JSON.PARSE() IN NODE.JS

Pravesh Singh

Total Post:101

Points:709
Posted by  Pravesh Singh
 6184  View(s)
Ratings:
Rate this:
Hi Guys

I am having the following string object and I want to create a JSON object out of it and store it in MongoDB.

    var strMYDATA = '{"Name":"ABC","SignedDate" : ISODate("2013-04-02T09:07:42.282Z")}';
I tried the following to create a JSON object out of strMYDATA

    var JSONobj = JSON.parse(strMYDATA);
    console.dir(JSONobj);
Getting the following error:

undefined:1
 "Name":"ABC","SignedDate" : ISODate("2
                              ^
SyntaxError: Unexpected token I
    at Object.parse (native)
Could anyone know how to parse the string with ISO date field like above in Node.JS?

Note: Cannot change the strMYDATA content because it a serialized object received from other Node.JS restful API.

Thanks

  1. AVADHESH PATEL

    Post:604

    Points:4228
    Re: How to parse into JSON object using JSON.parse() in Node.js

    Hi Pravesh!


    Your JSON data is bad. You have unescaped strings in your data.

    var strMYDATA = '{"Name":"ABC","SignedDate" : "ISODate("2013-04-02T09:07:42.282Z")"}';

    should be

    var strMYDATA = '{"Name":"ABC","SignedDate" : "ISODate(\"2013-04-02T09:07:42.282Z\")"}';

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!