473,657 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

escaping special characters in JSON

I have an application that relies on JSON, and I realized yesterday
that I when the user types at least some of these characters (most
likely all, haven't had time to verify) that the parser has problems:
, { } [ ] " '

How can I escape these characters so that it is in the file, but can
still be parsed?

For example:
{data: ["I want a quote, 'and so here it is', [with something in
brackets]'", "and {more data}"]}

I also had a problem when a newline ('\n') was entered, so I turn those
into '<br>'.

Thanx for any help.

Apr 8 '06 #1
4 85009


James Black wrote:
I have an application that relies on JSON, and I realized yesterday
that I when the user types at least some of these characters (most
likely all, haven't had time to verify) that the parser has problems:
, { } [ ] " '

How can I escape these characters so that it is in the file, but can
still be parsed?


The JSON libraries should do all escaping for your, for instance for
JavaScript <http://www.json.org/js.html> there is the JSON.stringify
function which should do all escaping.
And if you want to create JSON with other programming languages then
there are libraries as well.

Note that brackets [] and {} inside of string literals do not need to be
escaped.
--

Martin Honnen
http://JavaScript.FAQTs.com/
Apr 8 '06 #2
Thank you for the response.

I am not using the stringify function, so I will try using that on
Monday and see if the problem goes away.

Apr 9 '06 #3
James Black wrote:
I have an application that relies on JSON, and I realized yesterday
that I when the user types at least some of these characters (most
likely all, haven't had time to verify) that the parser has problems:
, { } [ ] " '

How can I escape these characters so that it is in the file, but can
still be parsed?

For example:
{data: ["I want a quote, 'and so here it is', [with something in
brackets]'", "and {more data}"]}


Of the characters in the set

, { } [ ] " '

the only one requiring escapement is the ". This can be done by giving it a \
prefix.

http://JSON.org/
Apr 9 '06 #4
Thank you. On Monday I will try these ideas.

I find JSON to be the best way for my Ajax application to transfer
data, but I am still getting used to it.

Apr 10 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1980
by: Reply Via Newsgroup Thanks | last post by:
Folks, This questions is directed towards PHP/MySQL folk and relates to escaping hooks, apostraphe's and other characters that can create a security hole when writing to databases/files. I've been reading http://ca2.php.net/manual/en/function.get-magic-quotes-gpc.php and just need to confirm a couple of things: If I have magic_quotes_gpc on, and I use addslashes() - Does this in effect cause me to take security one step forward, and...
0
2572
by: Simon Hawkins | last post by:
Just wondering... ... Is there a class or method in the API that will automatically 'escape' all the HTML special characters in a String, or a file? Something like the 'URLEncoder' class where the space character " " is converted into a plus sign "+", for URL complience.... Example: String myString = "<TD VALIGN=\"top\" CLASS=\"NavBarCell3\"><FONT SIZE=\"-2\">";
0
1508
by: SeanGerman | last post by:
Howdy, Is there a way to explicitly escape special characters (particularly & (&amp;) and ' (&apos;)) when writing a data set with the WriteXml method? I know this is supposed to happen automatically (according to MS), but in my case, it doesn't. I'm pulling data from MS-SQL with c#, filling data tables with SqlDataAdapter and a query, adding tables to a DataSet, then WriteXml to a file.
4
9169
by: Jon | last post by:
Hi, I used XslCompiledTransform with the following Xsl file. The <xsl:text disable-output-escaping="yes"does not work when using XslCompiledTransform to do the trnasform (namely the output contain < not <), while it works when using MSXML2 to do the transform. Does anyone have the same problem and how to make the escape work? Thanks. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
12
3781
by: Logos | last post by:
Yes, eval is a tool of the devil and I'll burn for using it. However, in this instance it's quite handy and I'm quite lazy. So, here's a weird one, and I'm wondering if anyone has a workaround. I am pulling data off a server via AJAX, and some of that data has non-English characters in it. The data is in the form of a json: {"exitCode":1,"className":"clientRecords" ,"strAccountId":"100" ,"strName":"Dr. Gary A. Martin"
3
5379
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in
1
2372
by: =?Utf-8?B?bWljaGFlbHJp?= | last post by:
We are dynamically changing the title property of nodes in our sitemappath to reflect the names of products by handling the SiteMapResolve event. We have some unicode characters in our product names, stored in our db using escape characters like: "è" When we set the SiteMapNode.Title to that value it is escaped by the sitemappath to "è" which results in the ampsand and code being printed out vs the desired character of: è
4
3762
by: laredotornado | last post by:
Hi, I'm using PHP 5 on Linux. I have a function that checks for file type by doing the following: $cmd = "file $p_input_file"; exec("$cmd 2>&1", $output, $return); However if the file "$p_input_file" contains spaces, the command doesn't work properly (I would need to insert a "\" before the space
7
4109
by: wannymahoots | last post by:
optparse seems to be escaping control characters that I pass as arguments on the command line. Is this a bug? Am I missing something? Can this be prevented, or worked around? This behaviour doesn't occur with non-control characters. For example, if this program (called test.py): from optparse import OptionParser parser = OptionParser() parser.add_option("-d", dest="delimiter", action="store")
0
8838
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8613
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7351
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2740
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.