0

I have a string:

{
  "key1": "val1",
  "key2": "",
  "keyObj": {
    "key3": 300,
    "key4": 259200
  }
}

I'm trying to convert it to the javascript object with:

 JSON.parse(my_str.toString());

But i'm getting error: SyntaxError: Unexpected token o

Why?

Thank you

2
  • How you define "my_str"? Commented Nov 29, 2013 at 8:48
  • Post all your code. Based on the posted info, it works just fine. Commented Nov 29, 2013 at 8:49

1 Answer 1

2

my_str seems to be already an object. So you just don't need to use `JSON.parse.

Because, probably, my_str.toString() is equal to

[object Object]
 ^---------------- Unexpected token o

If you use jQuery.ajax remember that jQuery convert automatically JSON input data if it match coherent header

Content-type: application/jso

and/or if you set the .ajax option

dataType:'json'

if you're declaring manually my_str = then simply you should add quotes around the string (and put it into a single line or use some tip in order to do multiline string)

var my_str = '{"key1":"val1","key2":"","keyObj":{"key3":300,"key4":259200}}';

but doing manually has not much sense.

Sign up to request clarification or add additional context in comments.

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.