0
var str = "{'a':'Your's'}";
JSON.parse(str);

My server response like above string. Not able to parse as JSON. It is as example text. While am parsing I got error as below:

JSON.parse(str);
VM1951:1 Uncaught SyntaxError: Unexpected token ' in JSON at position 1(…)(anonymous function) @ VM1950:1

The same question already raised by someone. click here to see. But, not helpful. Please help on this. Thanks in advance.

Jan Schultke
  • 17,446
  • 6
  • 47
  • 96
Nisar
  • 828
  • 1
  • 10
  • 28

2 Answers2

3

TL;DR JSON only supports double quotes. Please fix the error on the server, if this is possible.


JSON required double quotes, the single quotes are therefore not conform to the standard. There may be parsers, that support them anyway, but you can never rely on that. For more details on the JSON syntax, you may have a look at http://www.json.org/.

Besides this, the input string "{'a':'Your's'}"; is totally invalid itself, if the single quotes would be valid. The ' in Your's is breaking the string literal, the following s is outside the string and the next ' is opening a string, that contains } but is never closed by another '.

The correct syntax would be '{"a":"Your\'s"}'. If you received the string and cannot correct the server output, you may try to replace all ' by ", but you will have problems with single quotes inside your payload strings. By far the easiest - and most stable(!) - fix should be correcting the server output instead of correcting the defect output on the client.


Converting on the client with the following code may be a good idea at the first thought, but would corrupt payload strings with single quotes in it.

replaceInString = function(fullString, search, replacement) {
    return fullString.split(search).join(replacement);
};

var json = replaceInString("{'a':'Your's'}", "'", '"');

If you can be sure, there are no whitespace characters outside the payload and also there are no line breaks, you could use the following function. But also only, if you are sure, the search patterns are not in the payload strings.

var json = "{'a':'Your's'}";

replaceInString = function(fullString, search, replacement) {
    return fullString.split(search).join(replacement);
};

json = replaceInString(json, "{'", '{"');
json = replaceInString(json, "'}", '"}');
json = replaceInString(json, "':", '":');
json = replaceInString(json, ":'", ':"');
json = replaceInString(json, "',", '",');
json = replaceInString(json, ",'", ',"');
json = replaceInString(json, "['", '["');
json = replaceInString(json, "']", '"]');

But using this code, e.g. the JSON `following JSON string would be corrupted.

{'mathTerm':'x=1-[2+A']'}

To be clear: Code like this gets you over a bump on the road to develop , test or learn something. But this is not a durable fix. Contact the server developer to fix his implementation and remove your client side fix before going to production.

Cellcon
  • 1,245
  • 2
  • 11
  • 27
  • Absolutely right. While storing on server, the string as {"a":"Your's"}. After getting response from server is "{'a':'Your's'}". – Nisar Dec 02 '16 at 09:30
  • Thanks your answer is right. At the same time, as u mentioned it would be corrupted – Nisar Dec 02 '16 at 10:03
2

JSON keys and values must be enclosed in double quotes (") instead of single quotes (')

This is correct:

var str = '{"a":"Your\'s"}';
JSON.parse(str);
laika
  • 1,319
  • 2
  • 10
  • 16
  • Thanks for the response., you are absolutely right. While storing on the server, the string was like '{"a":"Your's"}'. After getting response from server, the response is "{'a':'Your's'}". My question is that how to convert the response? – Nisar Dec 02 '16 at 09:33
  • @Nisar fix it on the server side – laika Dec 02 '16 at 09:37
  • do you mean to say your server is changing the actual data you stored ? – mrid Dec 02 '16 at 09:37
  • @mrid My server is changing actual data. I will check it on my server side. – Nisar Dec 02 '16 at 09:43