Search code examples
coldfusion

Problems Accessing Nested Structs in Coldfusion


I'm trying to parse out an Azure DevOps API to get work item detail as documented here in the Azure DevOps API Docs.

I can successfully call it but I'm struggling to access fields in the second nested struct. It returns the following, as a brief example:

enter image description here

My code:

<cfset result="#DeserializeJSON(cfhttp.filecontent)#">

<cfoutput>
  #result.id#, #result.rev#, 
  <!---<cfdump var="#result.fields#">--->
  <cfloop collection="#result.fields#" item="f">
    <cfif not isStruct( result.fields[f] )>
      <cfif not isArray( result.fields[f])>
        #encodeForHTML(f)#, #encodeForHTML(result.fields[f])# <br/>
      </cfif>
    </cfif>
  </cfloop>
</cfoutput>

The part that I'm getting hung up on is how to reference System.AreaPath directly, for example. I've tried to reference it like:

#result.fields[f].[System.AreaPath]# but I get errors. Is it possible to reference this nested struct by name directly, as well as the other children within it like System.TeamProject?

Thank you in advance.


Solution

  • I think @mykaf is right. If you're looping over the struct #result.fields# with 'f' as your item, then f will be the struct members as the loop iterates. For example, System.AreaPath, System.TeamProject, etc....

    So, if you're trying to display the value in "System.AreaPath" inside the loop, it would be:

    #result.fields[f]#
    

    Where your original question led us to think you wanted to "reference System.AreaPath directly". If that were the case, referencing it outside the loop, it would be:

    #result.fields["System.AreaPath"]#