24
Feb

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:

@ReplaceSubstring(XXX;"\\":"\"":"'":@NewLine;"&#92;":"\\\"":"&#39;":"<br>")

or

@ReplaceSubstring(XXX;"\\":"\"":"'":@NewLine;"&#92;":"\\\"":"&#39;":"")

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.

References

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

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. You can leave a response, or trackback from your own site.

One comment

appliance repair in murrells inlet
 1 

I have read so many articles or reviews on the topic of the blogger lovers but
this article is really a nice piece of writing, keep it up.

July 18th, 2013 at 05:59

Leave a reply

Name (*)
Mail (will not be published) (*)
URI
Comment