1

The following data is retrieved from the MongoDB

console.log(x)

The output

{ _id: 54473495721e8a7386959897,
  tag: 'java',
  data: 
   [ 
     { view: '2342343', date: '2001/1/25' }
   ]
}

While parsing using JSON.parse

var dataJson = JSON.parse(x);

it thwors the below error

undefined:1
{ _id: 54473495721e8a7386959897,
  ^
SyntaxError: Unexpected token _
    at Object.parse (native)
1
  • 1
    Looks x is already an Object. Dont need to JSON.parse Commented Oct 27, 2014 at 4:48

2 Answers 2

1

white space gives error in JSON replace it first

x = x.replace(/\s/g, '');
x = JSON.stringify(x);
x = JSON.parse(x);
Sign up to request clarification or add additional context in comments.

Comments

0

It is already an javascript object so dont need parsing, do you want to convert it as json string?

if yes you can convert to JSON with

  JSON.stringify(x);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.