LogoLogo
Printed BookDownload BookCFML SupportCFML Videos
  • Introduction
  • Intro
    • Welcome
      • Author
      • About This Book
    • What is ColdFusion (CFML)
    • CommandBox CLI
    • Instructions & Interpreters
  • CFML Language
    • Syntax
    • Comments
    • Variables
    • Variable Scopes
    • Operators
    • Null & Nothingness
    • Strings
    • JSON
    • Numbers
    • Arrays
    • Structures
    • Database Queries
    • Conditionals
    • Exception Management
    • Components
      • Properties
      • Functions
      • Static Constructs
      • Final Constructs
      • Abstract Constructs
      • Interfaces
    • Closures
    • Code Locking
    • Threading
    • Includes
    • Java Integration
  • Beyond The 100
    • Beyond The 100
    • Application.cfc
    • File Handling
    • Image Manipulation
    • HTTP/S Calls
    • Sending Emails
    • Asynchronous Programming
  • Extra Credit
    • MVC
    • Dependency Injection
    • Security Guide
Powered by GitBook

Support Us

  • Become a Patreon
  • Contribute
  • CFCasts

Social Media

  • LinkedIn
  • Facebook
  • Twitter

Ortus Solutions, Corp

On this page
  • Serialize
  • Key Casing
  • Possible Casting Issues
  • Deserialize
  • Is this JSON?

Was this helpful?

Edit on GitHub
  1. CFML Language

JSON

JSON all things!

PreviousStringsNextNumbers

Last updated 2 years ago

Was this helpful?

CFML supports native JSON support via several key functions and some member functions.

Serialize

CFML gives us the serializeJSON() function to convert any piece of data to its JSON representation ()

serializeJson(
 var
 [, serializeQueryByColumns = false ]
 [, useSecureJSONPrefix = false ]
 [, useCustomSerializer = false ]
)

Pass in any complex or simple variable to the var argument and JSON will be produced:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( serializeJSON( person ) );

If you are in Lucee, you can even use the toJSON() member function:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( person.toJSON() );

Key Casing

By default CFML will convert the keys in a struct to uppercase in the result JSON document:

person = { name = "Luis Majano", company = "Ortus Solutions", year = 2006};
writeOutput( serializeJSON( person ) );

// Will become
{ "NAME" : "Luis Majano", "COMPANY" : "Ortus Solutions", "YEAR" : 2006 }

If you want to preserve the key casing then wrap them in double/single quotes and define the case:

person = { 
    'Name' = "Luis Majano", 
    'company' = "Ortus Solutions", 
    'year' = 2006
};

// Will become
{ "Name" : "Luis Majano", "company" : "Ortus Solutions", "year" : 2006 }

Possible Casting Issues

myStruct = { "zip"="00123" };
myStruct.setMetadata( { "zip": "string" } );
writeOutput( serializeJSON(myStruct) );

Deserialize

deserializeJSON(
 json
 [, strictMapping = true ]
 [, useCustomSerializer = false ]
)

Just pass a JSON document, and off we go with native structs/arrays/dates/strings and booleans.

if( isJson( mydata ) ){
    return deserializeJSON( data );
}

person = deserializeJSON( '{"company":"Ortus","name":"Mr OrtusMan"}' );
writeOutput( person.company );

This function can also be used as a member function in any string literal:

var deserializedData = myjsonString.deserializeJson();
var data = '[]'.deserializeJson();

Is this JSON?

isJSON( "[ 1, 2, 3 ]" )

Adobe ColdFusion may incorrectly serialize some strings if they can be automatically converted into other types, like numbers or booleans. One workaround is to use a CFC with to specify types. Another workaround is to prepend Chr(2) to the value and it will be forced to a string, however, that is an unofficial/undocumented workaround. A more formal workaround is to call setMetadata() as a member function on a struct to force a type:

The inverse of serialization is deserialization (). CFML gives you the deserializeJSON() function that will take a JSON document and produce native CFML data structures for you.

CFML has a function to test if the incoming string is valid JSON () or not: isJSON()

https://cfdocs.org/serializejson
cfproperty
https://cfdocs.org/deserializejson
https://cfdocs.org/isjson