DeserializeJSON()
Converts a JSON (JavaScript Object Notation) string data representation into CFML data, such as a struct or array.
DeserializeJSON( JSONVar=string, strictMapping=boolean, format=string );
Returns: any
Argument | Description | Default |
---|---|---|
JSONVar
string,
required
|
A string that contains a valid JSON construct, or variable that represents one. Alias: json, data |
|
strictMapping
boolean,
optional
|
A Boolean value that specifies how to convert the JSON
|
true |
format
string,
optional
|
The format of the input string. Possible values are:
Introduced: 6.2.0.14 |
json5 |
Examples
json = '{
"stringValue":"a string",
"arrayValue": ["a","b","c"],
"booleanValue":true,
"numericValue": 42
}';
myStruct = deserializeJson(json);
writeDump(myStruct);
Credit to Adam Cameron for suggesting the example
Strict Mapping with Queries
q = queryNew(
"id,name",
"numeric,varchar",
{
id: [1,2,3],
name: ['Neo','Trinity','Morpheus']
}
);
dump(q);
dump( var=deserializeJSON( serializeJSON(q, 'column'),true ),
label="'strictMapping TRUE'" );
dump( var=deserializeJSON( serializeJSON(q, 'column'),false ),
label="'strictMapping FALSE'" );
<span class="nv">q</span><span class="o">=</span> <span class="p">{</span> <span class="nv">nested1</span><span class="p">:</span> <span class="nv">q</span><span class="p">,</span> <span class="nv">nested2</span><span class="p">:</span> <span class="p">{</span><span class="nv">q3</span><span class="p">:</span> <span class="nv">q</span><span class="p">}</span> <span class="p">};</span>
<span class="nf">dump</span><span class="p">(</span> <span class="k">var</span><span class="o">=</span><span class="nf">deserializeJSON</span><span class="p">(</span> <span class="nf">serializeJSON</span><span class="p">(</span><span class="nv">q</span><span class="p">),</span><span class="nv">false</span> <span class="p">),</span>
<span class="nv">label</span><span class="o">=</span><span class="s2">"'strictMapping FALSE *nested queries*'"</span> <span class="p">);</span>
See also
- JavaScript
- JSON
- Parsing
- Strings
- Ajax features
- Serialize()
- SerializeJSON()
- string.deserializeJSON()
- <cfwddx>
- Search Issue Tracker
- Search Lucee Test Cases (good for further, detailed examples)