Thomas
Thomas

Reputation: 85

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.

Upvotes: 0

Views: 959

Answers (3)

Thomas
Thomas

Reputation: 85

This is what I finally came up with. Hoping it can help other beginners like me.

<cfset pbdata=deserializeJSON(httpResp.filecontent)>
<cfoutput>

<cfloop from="1" to="#arrayLen(pbdata.data)#" index="i">


#i#: #pbdata.data[i][9]#  : #pbdata.data[i][10]#<br />
</cfloop>

</cfoutput>

The JSON file is an array of an array and it is inside the structure "data". So you have to loop thru the 2d array and pull out elements [9] and [10] which are the dates and the winning numbers.

Upvotes: 0

Mark A Kruger
Mark A Kruger

Reputation: 7193

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. :)

Upvotes: 1

Sachin Verma
Sachin Verma

Reputation: 11

Try this code

<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)>
<cfset winDate = pbdata.data[1][9]>
<cfset winnerList = pbdata.data[1][10]>
<cfoutput>
   Winner for the #winDate# are #winnerList#
</cfoutput>

Upvotes: 0

Related Questions