Thomas Thomas - 3 months ago 42
JSON Question

Coldfusion returning JSON data

Happy Labor Day Everyone.

Trying to return usable data from JSON file.

This is what I have so far:

<cfhttp url="https://data.ny.gov/api/views/d6yy-54nr/rows.json?accessType=DOWNLOAD" method="get" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfset pbdata=deserializeJSON(httpResp.filecontent)>
<cfdump var="#pbdata#">


Which returns this:

JSON OUTPUT

How can I just get column 9 and 10 into something usable.

My end goal would be to have a drop down field of dates and it will return the winning numbers for that data.

Thank you for your time.

<cfhttp url="https://data.ny.gov/api/views/d6yy-54nr/rows.json?accessType=DOWNLOAD" method="get" result="httpResp" timeout="120">
<cfhttpparam type="header" name="Content-Type" value="application/json" />
</cfhttp>
<cfset pbdata=deserializeJSON(httpResp.filecontent)>
<cfoutput>
<cfloop array="#pbdata#" index="i">
<cfloop array="#i#" index="k">
#i[k]#
</cfloop>
<br/><br/>
</cfloop>

</cfoutput>


I tried this to loop thru the array but I get the error "Object of type class coldfusion.runtime.Struct cannot be used as an array".

I'm having a real hard time learning data handling in CF can anyone recommend good tutorials. Was also thinking are getting an online tutor. But they don't seem as common for ColdFusion. Any advise would be appreciated.

Answer

Change this:

<cfloop array="#pbdata#" index="i"> 
            <cfloop array="#i#" index="k"> 
               #i[k]#
            </cfloop>  
            <br/><br/>
 </cfloop>

To this:

<cfloop array="#pbdata.data[1]#" index="i"> 

               #pbdata.data[1][i]#
            <br/><br/>
 </cfloop>

And you will probably see what you are looking for. You have to drill in to the Array - it's a member of an array which is part of a structure that looks to be "data". You'll have to experiment a bit. :)