Search code examples
javascriptjsonnode.js

How to convert the given data into JSON?


I want convert the following data into JSON object. But when is used JSON.parse() command it returns:

var r = JSON.parse(t)
    SyntaxError: Unexpected token o in JSON at position 1

Here is sample data string which is to be converted

 var t = { message: 
       [ { timestamp: 1522007930599,
           tags: [Array],
           _id: '5aacb7cc0281b558debacf26',
           message_link: 'String',
           __v: 0 },
       ] }

Solution

  • JSON.parse converts from JSON to a JavaScript data structure.

    To go the other way you need JSON.stringify