Sprose
Sprose

Reputation: 1345

Using JSON data in ColdFusion

I have JSON data like the following

{
"category" : [
    {
        "name" : "digital stickers",
        "products" : [
            {
                "name" : "round digital stickers",
                "urlPublic" : "index.cfm/digital-stickers/round-stickers",
                "urlPortal" : "tab=round digital stickers"
            },
            {
                "name" : "square digital stickers",
                "urlPublic" : "index.cfm/digital-stickers/square-stickers",
                "urlPortal" : "tab=square digital stickers"
            }
        ]
    },
    {
        "name" : "Litho stickers",
        "products" : [
            {
                "name" : "round litho stickers",
                "urlPublic" : "index.cfm/litho-stickers/round-stickers",
                "urlPortal" : "tab=round litho stickers"
            },
            {
                "name" : "square litho stickers",
                "urlPublic" : "index.cfm/litho-stickers/square-stickers",
                "urlPortal" : "tab=square litho stickers"
            }
        ]
    }
]
}

I have used DeserializeJSON(theData) as below

<cfset cfData=DeserializeJSON(theData)>

I have then set a data array to store the category values

<cfset dataArray = cfData.category>

and have spat them out in a loop

<cfloop array="#dataArray#" index="elem">
    <!--- print out value for demo purposes --->
    <cfoutput>
            <h3>#elem.name#</h3>

    </cfoutput>
</cfloop>

This is all working great and I see the 2 category headings - 'digital stickers' and 'litho stickers'.

What I want to do now is display the products in a list under each relevant category. So under 'digital stickers' to have 'round digital stickers' and 'square digital stickers', then under Litho stickers to have 'round litho stickers' and 'square litho stickers' etc.

I tried to create a new array as below

<cfset productArray = cfData.products>

but I got the error message 'Element PRODUCTS is undefined in CFDATA'

Then my plan was to set this loop inside the current loop to loop through the relevant products. I think I may need a separate array for each category to loop through the products in that specific category.

Any help would be greatly appreciated - thanks in advance!

Upvotes: 3

Views: 573

Answers (1)

Tushar Bhaware
Tushar Bhaware

Reputation: 2525

You are getting error because products are not under cfData. The products array is under cfdata.category[i].products. To achieve the desired result, you can do something like below:

<cfoutput>
    <h3>#elem.name#</h3>
    <cfloop array="#elem.products#" index="innerelem">
         #innerelem.name#<br>
    </cfloop>
</cfoutput>

Update:

Once you deserialize the JSON, it is just a normal structure. Your cfdata.category (dataArray) is a array of structures with name and products keys. The products is another array of structures.

When you loop through dataArray, in the elem variable you get a structure with name and products keys. Then you need to loop through products to get names within products which is what i am doing in the above code.

To understand the code, use cfdump at every stage.

  1. Within first loop, cfdump elem. You will get a structure of name and products.
  2. You access name by elem.name.
  3. Similarly you can access products by elem.products.
  4. If you cfdump elem.products, you will notice it is a array of structure.
  5. As you can access elem.products array, You can loop it as you have looped array cfdata.category. You have to do this inside the first loop as you are able to get access to elem.products within the first loop.
  6. Now in the second loop, you get your each struct within elem.products in the variable innerelem.
  7. cfdump innerelem in the second loop. You will see a struct with keys name, urlpublic and urlportal with their respective values.
  8. As you have access to this struct, You can get your names by innerelem.name.
  9. You did good with url.
  10. Use cfdump a lot to understand code and to debug the code. Also while developing a code. It will help you a lot.

Upvotes: 3

Related Questions