Skip to content

Instantly share code, notes, and snippets.

@cocolote
Created July 15, 2015 14:31
Show Gist options
  • Save cocolote/3424e9b0a28cfe11acc8 to your computer and use it in GitHub Desktop.
Save cocolote/3424e9b0a28cfe11acc8 to your computer and use it in GitHub Desktop.
JavaScript Object Notation JSON and ColdFusion

#JavaScript Object Notation (JSON) and CFML

###Creating JSON object

As you probably guessed JSON objects can be created from cf structures

<cfset myStruct = {
	items: {
		item1: {name: 'item1', price: 1000},
		item2: {name: 'item2', price: 12.50},
		itme3: {name; 'itme3', price: 15000}
	},
	users: {
		user1: {id: 1, email: 'user1@gmail.com'},
		user2: {id: 2, email: 'user2@gmail.com'},
		user3: {id: 3, email: 'user3@gmail.com'}
	}
} />

<!--- parse the struct into a JSON object --->
<cfset myJSON = serializeJSON(myStruct) />

Because the keys in the key/value pair waren't declared as strings the serializerJSON would change the case to upcase. So to keep the letter case that you want they keys should be declared as strings.

###Convert JSON to Structure

<cfset myJsonStruct = deserializeJSON(myJSON) />

###Validate String as JSON

<cfdump var="#isJson(myJSON)#" />
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment