5

I have followed this Adobe Help/DeserializeJSON documentation but it's giving me errors like Error in custom script module or Element COLUMNS is undefined in CFDATA. Any help is much appreciated. The error comes from cfData. Doing anything with cfData will cause some kind of errors. Dumping cfData works fine though. It shows all the correct data. Below is my code:

<cfhttp url="http://api.openweathermap.org/data/2.5/weather?zip=55101,us&appid=44db6a862fba0b067b1930da0d769e98" method="get" >
<!--- JSON data is sometimes distributed as a JavaScript function. 
The following REReplace functions strip the function wrapper. --->
<cfset theData=REReplace(cfhttp.FileContent, "^\s*[[:word:]]*\s*\(\s*","")> 
<cfset theData=REReplace(theData, "\s*\)\s*$", "")> 
<!---<cfdump var="#theData#" >--->
<!--- Test to make sure you have JSON data. --->
<cfif !IsJSON(theData)> 
    <h3>The URL you requested does not provide valid JSON</h3> 

    <!--- If the data is in JSON format, deserialize it. --->
<cfelse>   
    <cfset cfData=DeserializeJSON(theData)>
    <cfdump var=#cfData# >
    <cfset colList=ArrayToList(cfData.COLUMNS)> 
    <cfset weatherIdx=ListFind(colList, "weather")>
    <cfset descriptionIdx=ListFind(colList, "description")>
    <!--- Now iterate through the DATA array and display the data. --->
    <cfoutput>
        <cfloop index="i" from="1" to="#Arraylen(cfData.DATA)#">            
            <h3>Weather: #cfData[i][weatherIdx]#</h3>
            <h4>Discription: #cfData[i][descriptionIdx]#</h4>
        </cfloop>       
    </cfoutput> 
</cfif>
5
  • does cfData have a COLUMNS property? it isn't a query object...
    – Kevin B
    Jan 26, 2016 at 19:09
  • I'm a ColdFusion newbie but by looking at the Adobe documentation, didn't the DeserializeJSON function supposed to make it into rows and columns. The comments of the documentation: Parse the resulting array or structure and display the data. In this case, the data represents a ColdFusion query that has been serialized by the SerializeJSON function into a JSON structure with two arrays: an array column names, and an array of arrays, where the outer array rows correspond to the query rows, and the inner array entries correspond to the column fields in the row.
    – 2myCharlie
    Jan 26, 2016 at 19:21
  • Nope, that's not what it does. It's almost identical to javascript's JSON.parse, only it creates structs and arrays instead of objects and arrays.
    – Kevin B
    Jan 26, 2016 at 19:22
  • The Adobe docs aren't always that great. There is also cfdocs.org. The examples there tend to get right to the point.
    – Twillen
    Jan 26, 2016 at 19:54
  • Nice job on putting together a small, clear, "runnable" example to go along with the question :)
    – Leigh
    Jan 26, 2016 at 20:00

1 Answer 1

2

Looking at your dump, the result of deserializeJSON is a struct, not a query. You can test to see if weather exists in cfData using the structKeyExists() function. The code below runs for me without error:

<cfhttp url="http://api.openweathermap.org/data/2.5/weather?zip=55101,us&appid=44db6a862fba0b067b1930da0d769e98" method="get" >
<!--- JSON data is sometimes distributed as a JavaScript function.
The following REReplace functions strip the function wrapper. --->
<cfset theData=REReplace(cfhttp.FileContent, "^\s*[[:word:]]*\s*\(\s*","")>
<cfset theData=REReplace(theData, "\s*\)\s*$", "")>
<!---<cfdump var="#theData#" >--->
<!--- Test to make sure you have JSON data. --->
<cfif !IsJSON(theData)>
    <h3>The URL you requested does not provide valid JSON</h3>

    <!--- If the data is in JSON format, deserialize it. --->
<cfelse>
    <cfset cfData=DeserializeJSON(theData)>
    <cfdump var=#cfData# >
    <cfif structKeyExists( cfData, 'weather' ) AND isArray(cfData.weather)>
        <cfoutput>
         <cfloop index="i" from="1" to="#arrayLen(cfData.weather)#">
            <h3>Weather: #cfData.weather[i].main#</h3>
            <h4>Description: #cfData.weather[i].description#</h4>
        </cfloop>
        </cfoutput>
    </cfif>
</cfif>
3
  • Thank you so much for your help. That was it. I forgot it was a struct and not a query. The dump even listed it on the top showing as struct.
    – 2myCharlie
    Jan 26, 2016 at 19:25
  • 2
    @Charlie123 Also note that the REReplaces shouldn't be needed. I'm not sure what the Adobe example file looked like, but most JSON apis will return JSON.
    – Twillen
    Jan 26, 2016 at 19:30
  • Thanks, Twillen for the tip. That confuses me (since I'm a ColdFusion newbie) when I was looking at their documentation. I guess they really want to make sure it's JSON.
    – 2myCharlie
    Jan 26, 2016 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.