SerializeJSON()

Converts CFML data into a JSON (JavaScript Object Notation) representation of the data.

SerializeJSON( var=any, queryFormat=any, useSecureJSONPrefixOrCharset=any, compact=boolean );

Returns: String

Argument Description Default
var
any, required

A CFML data value or variable that represents one.

Alias: data

queryFormat
any, optional

Either a Boolean value that specifies whether to serialize CFML queries by Column, or one of the following formats as a String

  • 'struct'
  • 'row'
  • 'column'

Alias: serializeQueryByColumns

useSecureJSONPrefixOrCharset
any, optional

String characters that cannot be encoded by this charset get escaped, if not set the web charset is used.

Alias: charset, charsetName, useSecureJSONPrefix

compact
boolean, optional

If true, it does not use end-of-lines and indentation. Defaults to true.

Also commonly called pretty printing

Introduced: 6.0.0.405

No

Examples

  st = structNew();
  st.id = 1;
  st.Name = "Water";
  st.DESIGNATION = "Important source for all";
  st.data = [1,2,3,4,5];

json= serializeJSON(st); writeDump(json); writeDump(deserializeJSON(json));

Query Formats

    q = queryNew(
        "id,name",
        "numeric,varchar",
        {
            id: [1,2,3],
            name: ['Neo','Trinity','Morpheus']
        }
    );
    dump(q);
    dump( var=deserializeJSON( serializeJSON(q) ),
        label="default" );
     dump( var=deserializeJSON( serializeJSON(q, true), false ),
        label="true" );     
    dump( var=deserializeJSON( serializeJSON(q, true) ),
        label='serializeQueryByColumns');
    dump( var=deserializeJSON( serializeJSON(q, 'struct') ),
        label="'struct'" );
    dump( var=deserializeJSON( serializeJSON(q, 'row') ),
        label="'row'" );
    dump( var=deserializeJSON( serializeJSON(q, 'column') ),
        label="'column'" );

Pretty Printing

<cfscript>
    obj = {
        name: "lucee",
        versions: [ 5,6,7 ]
    }
</cfscript>
<cfoutput>
<pre>
<b>Default behavior</b>
#serializeJson(var=obj,compact=true)#

<b>Pretty Print (compact=false)</b> #serializeJson(var=obj,compact=false)# </pre> </cfoutput>

dfsdfds

See also