CF Coding Practices
  • General Coding Practices
  • Debugging JSON-based CFC Methods
    • Valid JSON Formatting
    • Validating JSON
    • Final Hints
  • How We Invoke Modals
  • Submit Handlers
  • Useful SQL Snippets
  • jQuery/JS How-Tos And Hints
  • jQuery dataTables Tricks
  • CFSCRIPT Snippets and Hints
  • StatusPage Integration
  • Windows Server Setup
  • Common CF Formatting Commands
  • CF Snippets & Hints
  • Developing With The Mura Platform
    • ContentRenderer.CFC
    • Iterators
    • Iterating Remote Feeds
    • Components
    • Nested Content and Interactive Page Assembly
    • Modules and Display Objects
Powered by GitBook
On this page

Was this helpful?

  1. Debugging JSON-based CFC Methods

Valid JSON Formatting

It should be noted that certain characters will need to be escaped in the JSON data for each jQuery DataTable column in order to prevent JSON errors. While JSON only mandates that forward slashes (/) be escaped by a backslash (\/), use of other characters like commas, single quotes, whitespace at the stat of a string, and double quotes can throw off the rendering of the JSON. As a result, the following conversions should be made for any content being returned in the JSON fields (jQuery dataTable columns):

  • Forward slashes need to be escaped with a backslash. This is commonly an issue with end tags (e.g. </li> would need to be converted to <\/li>)

  • Double quotes should be converted to &quot;

  • Single quotes should be converted to &#39;

  • Commas are generally ok, but can be converted to &#44;

  • Carriage returns, chr(13), and line feed characters, chr(10), must be converted or removed for valid JSON.

Here is an example cfc function for filtering content prior to JSON formatting:

private string function jsonFilter(required string inputText) {
	var returnString = '';
	var characterConversion = [['"','&quot;'],
		['/', '\/'],
		[',', '&##44;'],
		["'", "&##39;"],
		[':','&##58;'],
		[chr(13),''],
		[chr(10),'']];
	
	returnString = trim(arguments.inputText);
	for (character in characterConversion) {
    	returnString = replace(returnString,
    		character[1],
    		character[2], 
    		'all');
	}
	return returnString;
}
PreviousDebugging JSON-based CFC MethodsNextValidating JSON

Last updated 6 years ago

Was this helpful?