By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,677 Members | 1,890 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,677 IT Pros & Developers. It's quick & easy.

escaping special characters in JSON

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a


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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.