JSON character escaping function in classic ASP

If you are into advanced AJAX techniques you probably will stumble across JSON (www.json.org) which makes developing (and therefore the life) a lot easier when working with AJAX. If you already stumbled across and you use JSON within classic ASP you might find this article useful. It deals with escaping characters for JSON acording to the RFC4627#2.5

I haven’t found any snippet out there for classic ASP so we had to write our own. And so my work colleague – an old algorithm guru – just came up with this function which covers all the requirements specified in the JSON RFC.

The code snippet above will result in a javascript error because we are passing an apostrophe from ASP to Javascript which leads to an early closing of the string: foo:”some va”lue” (there is one apostrophe too much). The same thing happens for other characters as well. If you have line breaks, german umlaute, hidden special characters, etc. All those need to be escaped now in order that the javascript simple object notation (JSON) can interpret it. Thats the function which does the escaping (there are 3 more functions cause they’re used within the escapeJSON()):

So now we should be able to escape the strings for JSON like this and no error should occur...

&#91;asp&#93;

<script>

alert({foo:"<%= escapeJSON("some va""lue")%>"}.foo);

</script>

This stuff has been tested and works for all characters so everything can be transfered with JSON now I would suggest to group the whole functions into a class called JSON with a method escape() just for a nicer use. In my case its in a utility class StringOperations where I have several string manipulation methods. If you are interested you can download the full String class also which include the JSON escaping method:

8 comments on “JSON character escaping function in classic ASP”

I’m having a problem converting the string back to special characters. When I get data from JSON and the strings are escaped, and I need to put that data into a form field the uuxx numbers are still there instead of the special characters. Do you have, or know of any function that reverses this process?

Your code:
- Escapes characters unnecessarily (such as forward slash).
- Escapes using more characters than needed, making the JSON longer (using hex notation instead of simple backslash-escaping).
- Escapes every high Ascii character as u00__ which is not only longer than x__ but completely ignores the first byte of Unicode characters, making the result meaningless.
- Has goofy stuff like “- &hC2C0″ which is clearly an undefined operation to perform on Unicode between &hC380 and &hC3BF. Perhaps you’re matching character sets to character sets, but this is something you’re supposed to solve with fonts, not subtraction to turn one code into another. To make your preposterous claim “This stuff has been tested and works for all characters so everything can be transfered with JSON now” valid, you have to rip out this junk. I don’t care how elaborate a character map you use, you’ll still be altering characters, hardly the desire of most people who want to send Unicode characters through JSON from ASP.

I know this is a very old thread but to whom it may concern (I know somebody is going to read this and think a, just what I need..)..

Emtucifor your code is “FLUNKED”.. How can you publish this forever to be read on the internet.. The replacements in your string do not even work.. All special characters like horizontal TAB are Null.

Ik know ASP != VB but I needed it in VB and cleaned it up, included it in my Anything2JSON class..

I’m using a quick and dirty array, only initializing it once (you should use this as a class!).

It can replace the specials to UNICODE, optionally you can choose to replace , ‘ [ ] { } ; too.

You can encode any Scripting.Dictionary object, VB array, String, Integer, Double etc. etc. to a valid JSON format, it it is not going to look pretty, it does not do indenting, but it will be valid!

String to UNICODE is not internal in the Anything2JSON functions, first use JSONEscape if you want to use special characters.

This code is far from perfect and should be treated as an Alpha release. You are free to do anything you want with it according to the MIT license.

Drop a line if you spot a bug or something..

'##################################################################
' Class: cAnyThing2JSON v0.1
' Class for encoding strings to JSON in compatible UNICODE text
' Written by removed apon request
' License CC:
'
' Permission is hereby granted, free of charge, to any person
' obtaining a copy of this software and associated documentation
' files (the "Software"), to deal in the Software without
' restriction, including without limitation the rights to use,
' copy, modify, merge, publish, distribute, sublicense, and/or sell
' copies of the Software, and to permit persons to whom the
' Software is furnished to do so, subject to the following
' conditions:
'
' The above copyright notice and this permission notice shall be
' included in all copies or substantial portions of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
' EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
' OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
' NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
' HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
' WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
' FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
' OTHER DEALINGS IN THE SOFTWARE.
'##################################################################

Private Exceptions() As String
Private UNICODE_REP(8 To 125) As String

' Used for exception handling, only 1 is actually implemented,
' only included for the sake of a complete picture.
Private Function Exception(strException)
ReDim Preserve Exceptions(UBound(Exceptions)) As String
Exceptions(UBound(Exceptions)) = strException
Debug.Print "Exception in cAnything2JSON: " & strException
End Function

' Return an array of Errors/Exceptions
Public Function getError() As Variant
getError = getExceptions()
End Function

' Return an array of Errors/Exceptions
Public Function getExceptions() As Variant
getExceptions = Exceptions
End Function