1

deserialized json -> coldfusion struct with arrays

Here is Google calendar data retrieved from https://www.googleapis.com/calendar/v3/users/me/calendarList.

The response is in JSON, so I used the ColdFusion method deserializeJson to turn it into a CFML structure.

I cannot figure out the syntax to loop over this data. You'll see that within the CFML struct, 'items' is an array of calendars returned from Google, and every attempt to access the array within the struct generates a Java string conversion error or a syntax error, likely because I can't get the syntax correct.

Any help is very appreciated, thanks very much

2
  • 5
    Can you post what have you tried so far and also the JSON response so that it will be easier to test the solution. Commented Apr 12, 2016 at 14:44
  • 1
    Best place to start is cfloop collection and cfloop array. Also, check this example. Commented Apr 12, 2016 at 15:37

1 Answer 1

9

Some trial and error will help - you are already on your way. The root object you dumped out already but we'll do it here to represent the object you dumped out as "obj":

<cfset obj = deserializejson(googleCal)>

The first level is a struct so you would address them as follows:

#obj.etag#  // this is a string

items contains an array. So you have items[1-n] ... however many are in the array. This code would set myArrayObj as an array with 3 items (based on the dump above).

<cfset myArrayObj = obj.items>

You can verify using isArray:

#isArray(myArrayObj)#

Each array member contains a struct in turn so you can output:

#myArrayObj[1].accessRole#  // this would be a string

... And so on. Make a note that index item 3 in the array is a blank struct so you need to "check" to see if the struct is empty before you work with it's keys. Investigate "structKeyExists()" for that purpose.

If you want to deal with each of the "items" in a loop (a pretty typical choice) you would simply do a cfscript loop or cfloop using array as in:

<cfloop array="#myArrayObj#" index="i">

    <cfif structKeyExists(myArrayOb[i], "accessRole")>
         #myArrayObj[i].accessRole#
    </cfif>
</cfloop>

Hope this helps. Good luck!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, really helped me out, much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.