-2

json string is as below:

"{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40/207, \"Shiva residency\", Near iscon mega mall, S.G. Highway\"], \"obs\": [1999]}"

JSON.parse() is unable to parse this string to object. The problem is at \"Shiva residency\" in string. JSON can not parse internal double quote("") string.

Can anybody help me to resolve this issue.

CHIN2
  • 185
  • 1
  • 1
  • 9

4 Answers4

0

The problem is here:

["40/207, "Shiva residency", Near iscon mega mall, S.G. Highway"]

You need to add " to those strings:

["40/207", "Shiva residency", "Near iscon mega mall, S.G. Highway"]

Edit: If you need to use the whole string use the following:

"{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40/207, \\"Shiva residency\\", Near iscon mega mall, S.G. Highway\"], \"obs\": [1999]}"

Oren Bengigi
  • 994
  • 9
  • 17
  • Thanks, but I know the problem. Within the bracket, there is a single element of string, I can not divide it into multiple. – CHIN2 Feb 13 '18 at 09:47
  • In that case you need to use the following String: "{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40/207, \\"Shiva residency\\", Near iscon mega mall, S.G. Highway\"], \"obs\": [1999]}" – Oren Bengigi Feb 13 '18 at 10:32
0

I think you've got an error in escaping. I've got following string:

"{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40\/207\", \"Shiva residency\", \"Near iscon mega mall\", \"S.G. Highway\"], \"obs\": [1999]}"

For further problems I recommend to use online escaping tools like this.

myOi1
  • 11
  • 2
0

The problem is escaping string "Shiva residency" inside a string in double quotes that also inside a string in double quotes. Do not create JSON manually if possible. Use JSON.strigify on front or some lib like gson (for java) on back.

console.log(JSON.parse("{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40/207, \\\"Shiva residency\\\", Near iscon mega mall, S.G. Highway\"], \"obs\": [1999]}"))

Also, it's much easier to use single quotes in js code, as double quotes widely used in html and json. With single quotes, you will not need to escape everything.

Yurii
  • 126
  • 5
0

(Updated) The problem is here(Actual Result):

["40/207, "Shiva residency", Near iscon mega mall, S.G. Highway"]

You need to remove \" to those strings(Expected Result):

["40, Shiva residency, Near iscon mega mall, S.G. Highway"]

(Updated) Run this example, it's works correctly:

const str = "{\"d_obs\": 1999, \"f_name\": \"p_add\", \"f_str\": \"CCD\", \"d_left\": 48.9, \"d_pos_label\": \"YES\", \"left\": [48.9], \"cat\": [\"40, Shiva residency, Near iscon mega mall, S.G. Highway\"], \"obs\": [1999]}";

const obj = JSON.parse(str);
console.log(obj);

(Updated) Result will be:

{
  "d_obs": 1999,
  "f_name": "p_add",
  "f_str": "CCD",
  "d_left": 48.9,
  "d_pos_label": "YES",
  "left": [
    48.9
  ],
  "cat": [
    "40, Shiva residency, Near iscon mega mall, S.G. Highway"
  ],
  "obs": [
    1999
  ]
}
аlex
  • 5,426
  • 1
  • 29
  • 38
  • This will not work in my case. `"cat": [ "40", "Shiva residency", "Near iscon mega mall, S.G. Highway" ]` there is a single element in array, not multiple. I want to remove that unwanted double quote in string – CHIN2 Feb 13 '18 at 10:22