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.

Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195
neogeomat
  • 361
  • 3
  • 13

2 Answers2

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"
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

abcdef12
  • 1,033
  • 1
  • 11
  • 22