Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON.stringify() not escaping apostrophe

Tags:

json

...using JSON2.js and JQUERY

as you can see from the first image the object property customerReport.Title has an apostrophe. In the code you can see that I'm calling JSON.stringify() into reportAsJson string which still has the unescaped apostrophe.

the error returned by $.ajax() is {"Message":"Invalid object passed in, \u0027:\u0027 or \u0027}\u0027 expected. ...

Initially I'm just going to ban apostrophe's from the user, but I thought JSON.stringify() handled this or do I need to set some option????

Thanks

enter image description here

enter image description here

enter image description here

like image 944
kevcoder Avatar asked Sep 08 '11 14:09

kevcoder


People also ask

Does JSON Stringify escape quotes?

JSON. stringify does not act like an "identity" function when called on data that has already been converted to JSON. By design, it will escape quote marks, backslashes, etc. You need to call JSON.

How do you handle an apostrophe in JSON?

To use an apostrophe or a single quotation mark (') in a value, add another single quotation mark after the first one. Notice that the value for name in JSONCUSTOMER table is "O''HARA". The name value in JSONCUSTOMER table is saved as O'Hara.

How do you escape an apostrophe in a string?

Use escapeEcmaScript method from Apache Commons Lang package: Escapes any values it finds into their EcmaScript String form. Deals correctly with quotes and control-chars (tab, backslash, cr, ff, etc.). So a tab becomes the characters '\\' and 't' .

How do you escape a line break in JSON?

JSON strings do not allow real newlines in its data; it can only have escaped newlines. Snowflake allows escaping the newline character by the use of an additional backslash character.


1 Answers

You can avoid removing these apostrophes replacing them with an HTML entity ' - that's a single quot - and later decode HTML entities either in the client or server-side.

like image 80
Matías Fidemraizer Avatar answered Sep 20 '22 08:09

Matías Fidemraizer