Skip to main content
Inspiring
May 4, 2020
Answered

Working with JSON - Record Count & Parsing

  • May 4, 2020
  • 2 replies
  • 1694 views

So now I have been able to get DeserializeJSON JSON data from a google API like this
Let me know if a larger picture of this will help.

 

From this code

<cfhttp url="https://storage.googleapis.com/storage/v1/b/myabc/o?prefix=products/3D/"> 

 <cfset cfData=DeserializeJSON(cfhttp.FileContent)>

 

The products parameter changes and will yield different results


I have never worked with JSON data before like this.

Questions:

  1. What CF code can I write to determine if I have any records from the API?
  2. What CF code can I write to get a record count of the JSON results?
  3. What CF code can I write to extract certain fields from the JSON structure?
    For example, I need to get the all the results from the id fields in a list.
    This topic has been closed for replies.
    Correct answer BKBK

    cfData is a struct. It contains a key-value pair. The key is called items, and it's value is an array.

    Each array item is a struct. The keys in this struct are bucket, contentType, crc32c, etag, and so on. They constitute a standard, fixed set.

    Now, on to your questions:

    What CF code can I write to determine if I have any records from the API?

    <cfif structCount(cfData) gt 0>
    <!--- There are records from the API. However, the records might be empty.--->
    </cfif>

    What CF code can I write to get a record count of the JSON results?
    <cfif structCount(cfData) gt 0>
    <!--- Recordcount is arrayLen(cfData.items)--->
    <cfelse>
    <!--- Recordcount is 0--->
    </cfif>

    What CF code can I write to extract certain fields from the JSON structure?

    <!--- crc32c value of first item --->
    cfData.items[1]["crc32c"]

    <!--- bucket value of third item --->
    cfData.items[3]["bucket"]


    For example, I need to get the all the results from the id fields in a list.

    <!--- all the id fields --->
    <cfset IDarray=arrayNew(1)>
    <cfloop from="1" to="#arrayLen(cfData.items)#" index="n">
    <cfset IDarray[n]=cfData.items[n]["id"]>
    </cfloop>

     

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    May 7, 2020

    cfData is a struct. It contains a key-value pair. The key is called items, and it's value is an array.

    Each array item is a struct. The keys in this struct are bucket, contentType, crc32c, etag, and so on. They constitute a standard, fixed set.

    Now, on to your questions:

    What CF code can I write to determine if I have any records from the API?

    <cfif structCount(cfData) gt 0>
    <!--- There are records from the API. However, the records might be empty.--->
    </cfif>

    What CF code can I write to get a record count of the JSON results?
    <cfif structCount(cfData) gt 0>
    <!--- Recordcount is arrayLen(cfData.items)--->
    <cfelse>
    <!--- Recordcount is 0--->
    </cfif>

    What CF code can I write to extract certain fields from the JSON structure?

    <!--- crc32c value of first item --->
    cfData.items[1]["crc32c"]

    <!--- bucket value of third item --->
    cfData.items[3]["bucket"]


    For example, I need to get the all the results from the id fields in a list.

    <!--- all the id fields --->
    <cfset IDarray=arrayNew(1)>
    <cfloop from="1" to="#arrayLen(cfData.items)#" index="n">
    <cfset IDarray[n]=cfData.items[n]["id"]>
    </cfloop>

     

    WolfShade
    Legend
    May 4, 2020

    Well, the good news is that it almost looks like the API is returning the JSON in a format that you might be able to convert it into a query object, which would make things much easier, coz then you could just valueList that ID column and get all the IDs that way.

     

    But it's been a while since I coded anything for JSON.  If you can't convert it to a query object, you might still be able to iterate the JSON object.  I'm just not sure how.

     

    V/r,

     

    ^ _ ^