arcee123
arcee123

Reputation: 243

using SerializeJSON from cfm how do I just output the Data portion?

SerializeJSON(cfquery) 

creates a JSON that looks like this:

"COLUMNS":["POINT","LOCATION"],"DATA":[["41.322365,-71.679251","Ashaway...

How do I output the JSON Data only?

ie...

[["41.322365,-71.679251","Ashaway...

Upvotes: 2

Views: 1772

Answers (3)

user1286322
user1286322

Reputation: 321

I came across this same problem, I think. I'm sending a query to a browser through an ajax call and need to loop through the JSON string in order to display the info. ColdFusion's serializeJSON method outputs a 1-D array called COLUMNS and a 2-D array called DATA.

Let's assume your JSON string has come back through and ajax call into the "response" variable. So we have response.COLUMNS and response.DATA.

By using the column name and finding its index, you can pinpoint its data for a given row. From there you do whatever you need.

var point;
var location;
for(i=0; i<response.COLUMNS.length; i++){
    point=response.DATA[i][response.COLUMNS.indexOf('POINT')];
    location=response.DATA[i][response.COLUMNS.indexOf('LOCATION')];
}

Upvotes: 0

Jeremy Halliwell
Jeremy Halliwell

Reputation: 3385

Sample Query

q = QueryNew( '' );
point = [ "41.322365,-71.679251" ];
location = [ "Ashaway" ];
QueryAddColumn( q,"point",point );
QueryAddColumn( q,"location",location );

CF10+ (Using "for row in query" syntax)

data = [];
for( row in q ) {
    // append array of two values
    ArrayAppend( data, [ row.point, row.location ] );
}
data = SerializeJSON( data );

CF9.01

data = [];
for( row=1; row <= q.recordcount; row++ ) {
    rowdata = [];

    ArrayAppend( rowdata,q.point[ row ] );
    ArrayAppend( rowdata,q.location[ row ] );
    ArrayAppend( data,rowdata );
}
data = SerializeJSON( data );

Result:

[["41.322365,-71.679251","Ashaway"]]

Upvotes: 0

Lyndon
Lyndon

Reputation: 106

I don't think there is a convenient way to do this unless you can modify the code that is serializing the query; otherwise you are going to need to use some string manipulation. However, assuming you have access to the CF code that is serializing the query, this is a little unorthodox but does work:

<!---Serialize and Deserialize the cfquery to shortcut obtaining a Structure--->
<cfset queryAsStruct = DeSerializeJSON(SerializeJSON(cfquery))>
<!---Now serialize the data key of the struct--->
<cfset dataJSON = SerializeJSON(queryAsStruct.data)>

As I said, not the the prettiest maybe...but seems to get it done. There may be a more convenient/better practice way to convert cfquery to a struct, but this one resulted in fewest lines of code for me.

edit: Just thought to explain why this works. The JSON string when being deserialized by ColdFusion is not detected as a query object anymore, just a plain struct so the "DATA" key is created and populated. THis way you can access it as a key of the structure. When cfquery is a query object, while still a "struct" type of object it has special considerations that prevent you from accessing the data key directly (to my knowledge).

Upvotes: 9

Related Questions