Search code examples
javascriptjsonescapingspecial-characters

How to parse special characters in json?


I am having problems with parsing special characters in javascript objects.

The escaped number makes the problem and double quotation character causes problems for me :

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\"350"}]');
SyntaxError: Unexpected number
message: "Unexpected number"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\""}]');
SyntaxError: Unexpected string
message: "Unexpected string"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error

JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width="}]');
[
Object
]

Solution

  • Your input strings are not correctly escaped for JavaScript. Use double backslashes to escape double quotation marks:

    JSON.parse('[{"title":"Turpial 3 Beta Builds Available for Ubuntu","description":"<p><img width=\\"350"}]');