JSON is the format almost every modern API speaks, and ColdFusion has three functions that handle the conversion in both directions: serializeJSON to turn CFML data into a JSON string, deserializeJSON to turn a JSON string back into CFML data, and isJSON to check a string is actually valid JSON before trusting it.
This lesson wraps up Collections by connecting everything covered so far — arrays, structs, and queries — to the format you'll actually send and receive when talking to a REST API.
Learning Objectives
After completing this lesson, you'll be able to:
- Convert CFML structs, arrays, and queries into a JSON string with serializeJSON.
- Convert a JSON string back into CFML data with deserializeJSON.
- Validate a string is well-formed JSON with isJSON before parsing it.
- Control how a query serializes to JSON with the queryFormat argument.
- Explain what strictMapping does, and why key casing catches people off guard.
- Call a JSON API with cfhttp and work with the response.
serializeJSON — CFML Data to JSON
Pass in a struct, array, query, or simple value and get back a JSON string.
If a struct key isn't quoted when you create it — {name: "Priya"} instead of {"name": "Priya"} — ColdFusion uppercases it by default, so it serializes as "NAME" instead of "name". Quote your keys if the JSON's casing matters to whatever consumes it.
deserializeJSON — JSON to CFML Data
The reverse direction — a JSON string becomes a real CFML struct or array you can work with directly.
isJSON — Validate Before You Parse
deserializeJSON throws an error on invalid JSON — check isJSON() first whenever the string comes from outside your own code (an API response, user input, a file).
Serializing Queries: the queryFormat Argument
Queries don't have an obvious JSON shape, so serializeJSON's second argument controls it: "row" (the default) produces column names plus a 2D array of row data, "column" produces one array per column, and "struct" produces an array of one struct per row — usually the friendliest shape for a REST API response.
The default "row" format is compact but awkward for anything except another ColdFusion app to consume — if you're building an API for outside consumers, "struct" is almost always the better choice.
strictMapping — Getting a Query Back Out of JSON
deserializeJSON's second argument, strictMapping, defaults to true and turns every JSON object into a plain CFML struct. Set it to false and ColdFusion will detect JSON that matches its own "row"-format query shape and rebuild it as a real query object instead of a struct.
Adobe ColdFusion 2025 also added a fourth argument, preserveCaseForStructKey (default false) — set it to true to keep a JSON object's key casing exactly as received, instead of ColdFusion's normal case-insensitive handling.
Real-World Example: Calling a JSON API with cfhttp
cfhttp fetches the raw response, isJSON confirms it's usable, and deserializeJSON turns it into data you can loop through.
<cfscript>
httpService = new http(url = "https://api.example.com/users", method = "GET");
result = httpService.send().getPrefix();
if (isJSON(result.fileContent)) {
users = deserializeJSON(result.fileContent);
for (u in users) {
writeOutput(u.name & "<br>");
}
} else {
writeOutput("API did not return valid JSON");
}
</cfscript><cfhttp url="https://api.example.com/users" method="GET" result="result">
</cfhttp>
<cfif isJSON(result.fileContent)>
<cfset users = deserializeJSON(result.fileContent)>
<cfloop array="#users#" index="u">
<cfoutput>#u.name#<br></cfoutput>
</cfloop>
<cfelse>
API did not return valid JSON
</cfif>result.fileContent holds the response body as a string, whichever syntax you use to make the request.
Common Beginner Mistakes
Unquoted struct keys serializing in uppercase
{name: "Priya"} creates a struct key that ColdFusion stores as NAME by default, so serializeJSON outputs "NAME" instead of "name". Quote the key when you create the struct — {"name": "Priya"} — to preserve the casing you actually want.
A string value like "100" or "yes" serializing as a number or boolean
Older Adobe ColdFusion versions guess a string's type from its content, so {"phone": "123456789"} could serialize as {"phone":123456789} — dropping it from a quoted string to a raw number. This was fixed by default from the 2018 release onward; on anything older, force the type with structSetMetadata() or a CFC's cfproperty type.
Calling deserializeJSON without checking isJSON first
deserializeJSON throws an error on malformed input. Any time the JSON comes from outside your own code — an API response, a file, user-submitted data — check isJSON() first and handle the false case instead of letting the error crash the request.
Expecting a query back from deserializeJSON without setting strictMapping to false
With the default strictMapping (true), even query-shaped JSON becomes a plain struct. Pass false as the second argument if you specifically need ColdFusion to rebuild a query object.
Best Practices
- Quote struct keys when the data will be serialized to JSON, so the output casing matches what you intend.
- Use serializeJSON(query, "struct") when building an API response — it's the shape most non-ColdFusion clients expect.
- Always run isJSON() on external input before calling deserializeJSON on it.
- Wrap cfhttp calls in a try/catch — a network failure or non-200 response won't raise a CFML error on its own, but a malformed body will if you deserialize it blindly.
Interview Questions
What does serializeJSON's queryFormat argument control?
How a query object serializes to JSON: "row" (default) gives column names plus a 2D array of row values, "column" gives one array per column, and "struct" gives an array of one struct per row.
What's the default value of deserializeJSON's strictMapping argument, and what does it do?
It defaults to true, which converts every JSON object into a CFML struct. Setting it to false lets ColdFusion detect JSON in its own query-row shape and convert it back into a real query object instead.
Why would isJSON() return false for a string that looks like valid JSON to you?
Common causes are trailing commas, single quotes instead of double quotes around keys/strings, or the string being empty/not actually a string (e.g., already-deserialized data passed in by mistake).
Summary
In this lesson, you learned to convert CFML data to JSON with serializeJSON, back with deserializeJSON, and validate a string first with isJSON — plus the two gotchas that catch almost everyone at least once: unquoted struct keys serializing in uppercase, and strictMapping defaulting to true.
That's the whole Collections module — arrays, structs, lists, queries, and now JSON, everything you need to hold, shape, and exchange data in a ColdFusion application.
What's Next?
The next module covers Functions — built-in functions, writing your own with arguments and return values, and how variable scope works inside a function body.
- Arrays
- Multi-Dimensional Arrays
- Adding & Removing Array Elements
- Searching & Sorting Arrays
- Functional Array Operations (map, filter, reduce)
- Array Conversion & Aggregation Functions
- Structures (Struct)
- Adding, Removing & Checking Struct Keys
- Functional Struct Operations (each, map, filter, reduce)
- Sorting & Searching Structs
- Lists
- Searching, Sorting & Transforming Lists
- <cfquery>
- <cfqueryparam>
- Query of Queries
- Query Functions (queryNew, queryEach, queryMap, queryFilter)