Skip to main content
2Charlie
Inspiring
January 25, 2016
Answered

Documentation on making API calls, parsing, and formatting.

  • January 25, 2016
  • 1 reply
  • 1352 views

Will someone provide me information/links to making API calls, parsing, and/or formatting the JSON result in ColdFusion. If there are video tutorials, that'll be awesome.

    This topic has been closed for replies.
    Correct answer 2Charlie

    @2Charlie

    In the example from deserializeJSON , change each of the 3 occurrences of ListFind to listFindNoCase. I have reported this error to Adobe.


    It's not working. This is what is working..got it from StackOverflow. I assumed the deserializeJSON automatically turned into query but it was not. Looks like it's a struct.

    <cfif structKeyExists(cfData, 'weather') AND isArray(cfData.weather)>

            <cfoutput>

            Name: #cfData.name#

             <cfloop index="i" from="1" to="#arrayLen(cfData.weather)#">       

                <h4>Weather: #cfData.weather.main#</h4>

                <h4>Description: #cfData.weather.description#</h4>

            </cfloop>

            </cfoutput>

        </cfif>

    1 reply

    EddieLotter
    Inspiring
    January 25, 2016

    There are examples in the documentation. See the serializeJSON and deserializeJSON functions.

    Cheers

    Eddie

    2Charlie
    2CharlieAuthor
    Inspiring
    January 25, 2016

    Okay, so I was following the example for the deserializedJSON function and it's giving me the "Element COLUMNS is undefined in CFDATA." error.

    <cfhttp url="https://app.knowledgeowl.com/api/head/category.json" 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*$", "")>

    <!--- Test to make sure you have JSON data. --->

    <cfif !IsJSON(theData)>

      <h3>The URL you requested does not provide valid JSON</h3>

      <cfdump var="#theData#" >

      <!--- If the data is in JSON format, deserialize it. --->

    <cfelse>

      <cfset cfData=DeserializeJSON(theData)>

      <!--- 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. --->

      <!--- First, find the positions of the columns in the data array. --->

      <cfset colList=ArrayToList(cfData.COLUMNS)>

      <cfset idIdx=ListFind(colList, "id")>

      <cfset statusIdx=ListFind(colList, "status")>

      <!--- Now iterate through the DATA array and display the data. --->

      <cfoutput>

      <cfloop index="i" from="1" to="#Arraylen(cfData.DATA)#">

      <h3>Category: #cfData.DATA[cateIdx]#</h3>

      <h3>Name: #cfData.DATA[1][nameIdx]#</h3>

      </cfloop>

      </cfoutput>

    </cfif>

    2Charlie
    2CharlieAuthor
    Inspiring
    January 26, 2016

    So, does anyone know why I am getting the Element COLUMNS is undefined in CFDATA error? I'm following this tutorial, ColdFusion Help | DeserializeJSON.