Skip to main content
Inspiring
September 8, 2022
Question

Getting data from an array

  • September 8, 2022
  • 1 reply
  • 356 views

Hi folks. I am trying to get data from an array in a JSON file. I create the variable webhook from the JSON. I need to get the product_ID from the two arrays.  When I dump webhook to a file, it looks like this:

...

line_items:
[array]
1) [struct]
id: 14
image:
[struct]
id: 57
src: https://i0.wp.com/www.blah.com/wp-content/uploads/2022/07/block_logo_round.png?fit=1200%2C1200&ssl=1
meta_data:
[array]
1) [struct]
display_key: _reduced_stock
display_value: 1
id: 155
key: _reduced_stock
value: 1
name: Block & Tackle Smokehouse and Tavern
parent_name: undefined
price: 19
product_id: 56
quantity: 1
sku: [empty string]
subtotal: 19.00
subtotal_tax: 0.00

2) [struct]
id: 15
image:
[struct]
id: [empty string]
src: [empty string]
meta_data:
[array]
1) [struct]
display_key: _reduced_stock
display_value: 1
id: 156
key: _reduced_stock
value: 1
name: Paddle 
parent_name: undefined
price: 54
product_id: 82
quantity: 1
sku: [empty string]
subtotal: 54.00
subtotal_tax: 0.00

First, I'm setting count to the Array Length.

<cfset count = #ArrayLen(webhook.line_items)# />

 That does return the length correctly. Then I am looping over the results;

<cfloop from="1" to="#count#" index="C">
<!--- database code here --->
#webhook.line_items["#c#"].product_ID#
</cfloop>

I'm getting the "You have attempted to dereference a scalar variable of type class java.lang.Double as a structure with members."

Suggestions on what I am doing wrong?

Thanks in advance for any help!

Gary

    This topic has been closed for replies.

    1 reply

    BKBK
    Community Expert
    Community Expert
    September 9, 2022

    Try the following and see what happens:

    (I am assuming the loop is surrounded by cfoutput tags)

     

    <cfloop array="#webhook.line_items#" item="lineItem">
    #lineItem#<br>
    </cfloop>

     

    BKBK
    Community Expert
    Community Expert
    September 16, 2022

    Oh, and please let us know the result of trying that.