Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jsStringFormat() and apostrophe with JSON

So I am having an interesting problem with jsStringFormat() when trying to escape special characters for JSON. I am using the jQuery datatables plugin and doing an AJAX call to Coldfusion.

What appears to be happening is that jsStringFormat() is escaping the apostrophe character and putting \' in my JSON. According to JSON spec, the single apostrophe doesn't need escaping, thus it breaks.

Here is a sample of my JSON return

{
    "sEcho": 2,
    "iTotalRecords": 659,
    "iTotalDisplayRecords": 201,
    "aaData": [

        ["516", "", "54d 7h 12m", "02- Revenue", "", "Assist in validating error in JCA provided Discount Commission report", "Received", "Work Request", "Jan 1, 2012"],
        ["616", "", "16d 7h 12m", "02- Revenue", "", "Order/Install new POS Terminal at Katie\'s Workstation", "In Progress", "Work Request", "Oct 31, 2011"],
        ["617", "", "15d 7h 12m", "02- Revenue", "", "Replace #6081 POS Printer at Kim\'s Desk", "Received", "Work Request", "Oct 31, 2011"]
    ]
}

You can clearly see the \' inserted in the descriptions.

I really need to find a way to prevent jsStringFormat() from escaping the apostrophe.


UPDATE

So far, have this code for attempting to populate the aaData array. Right now I am getting nothing but commas so I know its looping properly, but not populating the data in the right places.

All of this is based off of datatables coldfusion datasource code http://www.datatables.net/development/server-side/coldfusion

<cfcontent reset="Yes" />

<cfset aaData = [] />
<cfset datasetRecords = [] />
<cfloop query="qFiltered" startrow="#val(url.iDisplayStart+1)#" endrow="#val(url.iDisplayLength)#">
<cfif currentRow gt (url.iDisplayStart+1)>,</cfif>
        <cfloop list="#listColumns#" index="thisColumn">
            <cfif thisColumn neq listFirst(listColumns)>,</cfif>
                <cfif thisColumn is "version">
                    <cfif version eq 0>"-"
                    <cfelse><cfset datasetData["#version#"] />
                    </cfif>
                <cfelse><cfset datasetData[""] = qFiltered[thisColumn][qFiltered.currentRow] />
                </cfif>
                <cfset ArrayAppend(datasetRecords, datasetData ) />
        </cfloop>
<cfset ArrayAppend(datasetRecords, aaData ) />
</cfloop>

<cfset record = {} />
<cfset record["sEcho"] = val(url.sEcho) />
<cfset record["iTotalRecords"] = qCount.total />
<cfset record["iTotalDisplayRecords"] = qFiltered.recordCount />
<cfset record["aaData"] = aaData />

<cfoutput><cfdump var="#record#"></cfoutput>
<cfoutput>#serializeJSON(record)#</cfoutput>
like image 604
Brian Avatar asked Oct 05 '11 22:10

Brian


People also ask

Why does my JavaScript string contain apostrophes in it?

The JavaScript string I build to execute on the HTMLViewer includes apostrophes to enclose the JSON which uses double quotes for the items. (The data for the form is entered in another application.

How is JSON syntax derived from JavaScript?

JSON syntax is derived from JavaScript object notation syntax: Data is in name/value pairs. Data is separated by commas. Curly braces hold objects.

How do you write a string in JSON?

In JSON, string values must be written with double quotes: Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. You will learn how to convert JavaScript objects into JSON later in this tutorial.

Can you use JSON with JavaScript?

JSON Uses JavaScript Syntax. Because JSON syntax is derived from JavaScript object notation, very little extra software is needed to work with JSON within JavaScript. With JavaScript you can create an object and assign data to it, like this: var person = { name: "John", age: 31, city: "New York" };


2 Answers

JSStringFormat is designed for escaping data for inclusion in JavaScript, not JSON. In JavaScript, a single quote is a character that needs to be escaped.

On the other hand, SerializeJSON is actually meant to output JSON, and complies with the JSON spec.

like image 93
John Flatness Avatar answered Oct 20 '22 00:10

John Flatness


i ran into the same issue with datatables. what i did to fix it was create the jsstringformat string and then do a replace on the returned string to remove all instances of \' with '.

<cfset thisColumnString = jsStringFormat(trim((qFiltered[thisColumn][qFiltered.currentRow])))>
<cfset thisColumnString = replacenocase(thisColumnString,"\'","'","all")>
"#thisColumnString#"
like image 21
josh Avatar answered Oct 19 '22 22:10

josh