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 ma [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.
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 ma [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.
- Column 207 is the space. What could be the control character. – neogeomat Commented Jan 24, 2016 at 11:25
- How are you getting the JSON string from server? Add that code also. – Tushar Commented Jan 24, 2016 at 11:30
2 Answers
Reset to default 4There 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"
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.