4 Replies Latest reply on Oct 6, 2016 11:03 AM by 2Charlie

    How to grab data through nested loops?

    2Charlie Level 1

      The problem seems to be that it never gets inside the loop in line 13. I'm not sure if that line of code is correct because it's giving me Error in custom script module before it gets inside the loop. I put a plain texts inside the loop and it never gets executed.

       

      <cfoutput>
          <cfset cfData = DeserializeJSON(artDetails)>
          <cfif structKeyExists(cfData, 'data')>
          <cfif not ArrayIsEmpty(cfData.data) >
              <cfloop index="i" from="1" to="#arrayLen(cfData.data)#">
                  <!---<cfdump var="#cfData.data[i].faqChildren.categories#" >--->
                  <cfdump var="#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45#" >
                  <!---<cfdump var="#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.object#" >--->
                  <h2>#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.object.name.en#(#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subCount#)</h2>
                  <p>url_hash: #cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.object.url_hash#</p>
                  <cfdump var="#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent#" >
                  <ul>
                      <cfloop index="p" from="1" to="#arrayLen(cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent)#" >
                          <li>#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent[p].object.name#</li>
                      </cfloop>
                  </ul>
              </cfloop>
              <cfelse>
              <p>No data found for <b>#hash#</b>.
              <p>
          </cfif>
          </cfif>
      </cfoutput>
      

       

      My dump of subcontent at line 11 shows the following and I want to get the name. Any help is much appreciated.

      subContent.png

        • 1. Re: How to grab data through nested loops?
          2Charlie Level 1

          Okay, I've narrowed down the issues but not sure why it's still having issues.

           

          <cfdump var="#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent#" >

          <p>Length: #arrayLen(cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent)# </p>

           

          The dump shows the following so I think there is an array but the output length shows the "Error in custom script module". Any thoughts?

           

          subContent-array.png

          • 2. Re: How to grab data through nested loops?
            2Charlie Level 1

            I did an IsArray(cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subconten) and it says no. So, if it's not an array then what is it based on the image I've uploaded above?

            • 3. Re: How to grab data through nested loops?
              2Charlie Level 1

              Okay, so I have a structure instead of an array. How do I retrieve all the name from this structure? I don't want to manually specify each ID in the dot notation to access the value. There must be some more efficient ways to retrieve all the data within a struct.

              • 4. Re: How to grab data through nested loops?
                2Charlie Level 1

                Okay, I found the answer to my question. Replace line 13-15 with the following and now it's giving me the name that I need.

                 

                <cfloop collection="#cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent#" item="id">
                <li>Cell Name: #cfData.data[i].faqChildren.categories.5798bea791121cc46fd77a45.subcontent[id].object.name#</li>
                </cfloop>