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

Rhyono
  • 2,370
  • 24
  • 39
0xAX
  • 19,709
  • 24
  • 113
  • 202

1 Answers1

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.

Community
  • 1
  • 1
Luca Rainone
  • 15,578
  • 2
  • 37
  • 51