I'm calling the smartsheet.com api and they will either return an object or an array, I'm able to process the object into a CF Query with the following code, but having trouble processing the array.
The JSON:
[
{
"id": 2070463980562308,
"name": "Sheet1",
"accessLevel": "OWNER",
"permalink": "https://app.smartsheet.com/b/home?"
},
{
"id": 2810804673243012,
"name": "Project Management",
"accessLevel": "OWNER",
"permalink": "https://app.smartsheet.com/b/home?"
},
{
"id": 3678697304680324,
"name": " - Dispatch Sheets",
"accessLevel": "OWNER",
"permalink": "https://app.smartsheet.com/b/home?"
}
]
My CF code:
<cfset jsonData = deserializeJSON(json.smartsheet.sheets.filecontent) />
<!--- Check we have records returned to us --->
<cfif arrayLen(jsonData.sheets)>
<!--- We want to provide the query with column names --->
<cfset strColType = '' />
<!--- To do this, we'll take the first result item... --->
<cfset sheets = jsonData.sheets[1] />
<!--- and get the list of keys from the structure. --->
<cfset thisKeyList = structKeyList(sheets) />
<!---
We now need to provide the column data type.
This example assumes everything is a VarChar.
Looping over the list of keys, we'll append a
datatype to the column type list defined earlier.
--->
<cfloop list="thisKeyList" index="listItem">
<cfset listAppend(strColType,'varChar') />
</cfloop>
<!---
Generate the new query, passing in the
column list, column type list and the data.
--->
<cfset qrySheets = queryNew(
thisKeyList,
strColType,
jsonData.sheets
) />
</cfif>
This code only works when I get an object back..not an array.
deserializeJSON(), JS object becomes CF struct, JS array becomes CF array. Nothing tricky to it.<cfdump>and debug your code.