-1

I have a json object with html markup and many escaped characters as follows,

[{
    "status": "PASS",
    "server_name": "sk-vbox",
    "start_time": "20150528 11:49:49 Hrs",
    "summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>",
    "end_time": "20150528 11:49:51 Hrs",
    "log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>",
    "suite": "Sg Tc Initiator Start Test",
    "error_msg": "-"
}]

I am trying to parse it using JSON.parse as follows:

var a = JSON.parse('[{"status": "PASS", "server_name": "sk-vbox", "start_time": "20150528 11:49:49 Hrs", "summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>", "end_time": "20150528 11:49:51 Hrs", "log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>", "suite": "Sg Tc Initiator Start Test", "error_msg": "-"}]')

It throws error as,

JSON.parse: expected ',' or '}' after property value in object at line 1 column 111 of the JSON data

I read here, one can escape double quotes by single backward slash but it doesnt work.

Community
  • 1
  • 1
Saravana Kumar
  • 394
  • 1
  • 3
  • 13
  • Since you're dealing with a string (markup) within a string (JSON) within a string (JavaScript), you'll need to escape the former in both of the others. Currently, the JavaScript string is consuming the single escape sequence, leaving just the quotations unescaped for the JSON string. `JSON.parse('"Logs"')`. – Jonathan Lonowski May 30 '15 at 16:19
  • this is the answer, i was looking for... thanks Jonathan... – Saravana Kumar May 30 '15 at 16:23

1 Answers1

1

You need to delimit your string. Think about it:

var a = JSON.parse("[{"status

what's wrong there? Look at the quotes...

You can either wrap the whole string in ' (if it is guaranteed to not contain any ' itself):

var a = JSON.parse('[{"status": "PASS", "server_name": "sk-vbox", "start_time": "20150528 11:49:49 Hrs", "summay_html": "<a href=\"#sk-vboxSgTcInitiatorStartTest\">Summary</a>", "end_time": "20150528 11:49:51 Hrs", "log_html": "<a class=\"log\" href=\"log.html#s1-s2-s1\">Logs</a>", "suite": "Sg Tc Initiator Start Test", "error_msg": "-"}]')

or you can delimit all " inside it (as well as double-delimiting your already-delimited HTML):

var a = JSON.parse("[{\"status\": \"PASS\", \"server_name\": \"sk-vbox\", \"start_time\": \"20150528 11:49:49 Hrs\", \"summay_html\": \"<a href=\\\"#sk-vboxSgTcInitiatorStartTest\\\">Summary</a>\", \"end_time\": \"20150528 11:49:51 Hrs\", \"log_html\": \"<a class=\\\"log\\\" href=\\\"log.html#s1-s2-s1\\\">Logs</a>\", \"suite\": \"Sg Tc Initiator Start Test\", \"error_msg\": \"-\"}]")
Alex McMillan
  • 17,096
  • 12
  • 55
  • 88