473,387 Members | 1,650 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 84988


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
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...
0
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...
0
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...
4
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...
12
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....
3
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...
1
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...
4
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...
7
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...

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.