2

enter image description here

I am trying to access tax_lines.item and tax_lines.price, but not successful. Here is what I run:

<cfif structKeyExists( jsonData, 'orders' ) AND isArray(jsonData.orders)>
    <cfloop from="1" to="#arrayLen(jsonData.orders)#" index="i">
        <cfset line_items = jsonData.orders[i].line_items>
        <cfif not arrayIsEmpty(#line_items#)>
            <cfloop array="#line_items#" index="i"> 
            ......
            ........
            <cfset item_tax_lines = line_items[i].tax_lines>                    
                <cfif not arrayIsEmpty(#item_tax_lines#)>
                ....
                </cfif>
            </cfloop>
        </cfif>
    </cfloop>
</cfif>
1
  • Can you elaborate on "not successful"? Error, wrong result, ...? Commented Sep 18, 2019 at 1:41

2 Answers 2

2

line_items[i].tax_lines

That causes an error because you're using an array loop. So the index variable i contains the current element of the array - not a position number. To access the tax_lines key, use i.tax_lines instead.

<cfif not arrayIsEmpty(#line_items#)> <cfloop array="#line_items#" index="i">

Not related to the error, but the arrayIsEmpty check isn't necessary. If the array is empty, the loop will do nothing. So you could simplify the loop code to just this:

<cfloop array="#jsonData.orders#" index="order">
    <cfloop array="#order.line_items#" index="line">
        do something with #line.tax_lines# here ....
     </cfloop>
</cfloop>
Sign up to request clarification or add additional context in comments.

2 Comments

I edited original image for you to see what I am trying to do. <cfloop array="#line_items#" index="line"> <cfloop from="1" to="#arrayLen(line.tax_lines)#" index="i"> <cfoutput> #i.title#<br /> #i.rate#<br /> #i.price# </cfoutput> </cfloop> </cfloop> results in Error: You have attempted to dereference a scalar variable of type class java.lang.Double as a structure with members.
Now you have the opposite problem :-) The inner most loop is a from/to cfloop. That type of cfloop populates the index variable i with a numeric position - not an array element. Unless you actually need the position for something, best to stick with the same loop type throughout, i.e. "array" loop. <cfloop array="#line.tax_lines#" index="curr_tax_line">#curr_tax_line.title# ... </cfloop>
1

I like Ageax's solution. I would just do it in <cfscript>

<cfscript>
for (order in jsonData.orders) {
   for (line in  order.line_items) {
       // fancy stuff with line.tax_lines#
   }
 }
</cfscript>

1 Comment

That'd be my preference too, but given the cfml in the question, and history, I decided to stick with what they're using.

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.