Characters to escape in JSON

   Posted by: Grumelo   in javascript, web

Several characters must be escaped in JSON data to avoid any problems while reading them in JavaScript.

These characters are :

  • ‘ single quote
  • ” quote
  • \ backslash
  • all control characters like \n \t

Lotus Notes Domino solution

If you are generating JSON in a Lotus Notes Domino view you can use the following command:




Unfortunately no library can be used in a Notes View. The solution above is only a quick win.

You may find Lotus Notes Script JSON library at json.org

JavaScript solution

The excellent jQuery plugin JSON Parser can help you to deal with JSON.
The method quoteString() is for sure the one you may need.

Other parsers can be found at json.org

Why using HTML characters code instead of JavaScript escape solution?

In JavaScript to escape a ‘ you should write \’
to escape a ” you should write \”
to escape a  you should write \\

If you are working in a Domino view you will have to write something like \\\\, it’s not very readable.

With the HTML character code you don’t have to worry anymore about how many \ you have to add.

&#39; or &quot; replace ‘
&#92; replace \
&#34; replace ”

With &#39; you save one more character compare to its equivalent &quot;
In a JSON with a lot of data it’s always good to optimize the size.


Evaluation: Pas terribleAssez bienBienSuper!Excellent! (4 votes)

Tags: , , , ,

This entry was posted on Tuesday, February 24th, 2009 at 12:54 and is filed under javascript, web. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.

Comments are closed at this time.