Search code examples
arrayscoldfusioncfloopcoldfusion-2018

ColdFusion 2018 processing an array of structs


I am having issues processing the inner array (variants). Getting Object of type class java.lang.String cannot be used as an array

enter image description here

<cfset jsonData = deserializeJSON(httpResp.fileContent) />
    
<cfset products = jsonData.products>

<cfoutput>

    <cfloop array="#products#" index="x">   
        #x.id# - #x.handle# <br>
                    
        <cfset variants = "variants">
        
        <cfloop array="variants" index= "i">
            #i.barcode#
        </cfloop>
        
    </cfloop>    

</cfoutput>

Solution

  • With help of RRK (not sure how to give him credit for it), I was able to make it work:

    <cfset jsonData = deserializeJSON(httpResp.fileContent) />     
    <cfset products = jsonData.products>
    <cfoutput>  
        <cfloop array="#products#" index="x">   
            #x.id# - #x.handle# <br>
                        
            <cfset variants = "#x.variants#">           
            <cfloop array="#variants#" index= "i">
                <cfif IsDefined("i.barcode") and i.barcode is not "">
                    #i.barcode# <br>
                </cfif>
            </cfloop>
            
        </cfloop>        
    </cfoutput>