0

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 },
       ] }
  • I should add that there is no such thing as JSON object, JSON stands for `JavaScript Object Notation`. i.e. a format to represent javascript objects as string. – Brahma Dev Mar 25 '18 at 20:20
  • Before you put the issue you must be search about it. Look at here https://stackoverflow.com/questions/4162749/convert-js-object-to-json-string?rq=1 and here https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON – Francis Rodrigues Mar 25 '18 at 21:54
  • As `t` is already a `JSON Object` . Why again `JSON.parse(t)` ? – Debug Diva Mar 26 '18 at 09:01
  • @RohitJindal — `t` is a JavaScript object, not a JSON object. Using `JSON.parse` is wrong, but not for the reason you suggest. – Quentin Mar 26 '18 at 10:33
  • Ya i am sorry,my bad for not researching beforehand – Vinayak Shrivastava Jun 09 '18 at 16:10

1 Answers1

1

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

To go the other way you need JSON.stringify

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335