• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Working with JSON - Record Count & Parsing

Participant ,
May 04, 2020 May 04, 2020

Copy link to clipboard

Copied

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.

weezerboy_0-1588608864838.png

 

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.

Views

1.4K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 07, 2020 May 07, 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 rec

...

Votes

Translate

Translate
LEGEND ,
May 04, 2020 May 04, 2020

Copy link to clipboard

Copied

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,

 

^ _ ^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 07, 2020 May 07, 2020

Copy link to clipboard

Copied

LATEST

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>

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation