1

I have saved this string in javascript

js_str= '{"id":"1","user_id":"1","cat_id":"1","name_bz":"Chitwan National Park","name_cf":"Gokarna","cf_lattitude":"27.525","cf_longitude":"87.56","boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\n","area":"989.948","forest_conditon":"Poor","natural_regeneration":"High","grazing_pressure":"Medium","forest_type":"Natural","wild_species_list":"Tiger, Leopard, Rhino","others":null}';

On using js_arr=JSON.parse(js_str) it gives

SyntaxError: JSON.parse: bad control character in string literal at line 1 column 207 of the JSON data

column 207 is the space after comma [27.3564, 85.3564]. The whitespace in this place is giving error. i cannot use regex to remove whitespace as it would replace all whitespaces.

2
  • Column 207 is the space. What could be the control character. Commented Jan 24, 2016 at 11:25
  • How are you getting the JSON string from server? Add that code also. Commented Jan 24, 2016 at 11:30

2 Answers 2

4

There is a \n in the wrong place or should be escaped. Try to change from:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\n"

to:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]"

or escape it to:

"boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\\n"
Sign up to request clarification or add additional context in comments.

3 Comments

That is not the problem. That character there is valid JSON.
I think the \n should be removed or escaped
the new line char needs to be escaped stackoverflow.com/questions/42068/…
4

Just add a \ before \n control character and all will be fine. JSON parser works like this.

js_str= '{"id":"1","user_id":"1","cat_id":"1","name_bz":"Chitwan National Park","name_cf":"Gokarna","cf_lattitude":"27.525","cf_longitude":"87.56","boundry":"[[27.0656,85.255],[27.3564, 85.3564],[27.98998, 85.6898]]\\n","area":"989.948","forest_conditon":"Poor","natural_regeneration":"High","grazing_pressure":"Medium","forest_type":"Natural","wild_species_list":"Tiger, Leopard, Rhino","others":null}';

JSON.parse(js_str)

Successfully tested code in Mozilla Firefox. enter image description here

1 Comment

the \n is coming as a string from database . I cannot change it. Is there another way.

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.