Get array from with an array and dictionary in JSON data

Hi all,

I have some JSON data (Woocommerce product data from rest api) which is multiple “layers” deep with sub, sub arrays and such.

I can access the initial dictionary in the main array but I am having trouble getting the data from arrays within that first layer of dictionary’s.

I want to add the data to a local database and I can do that with the main section, the first level data such as ID, Name etc as I have tried the classic framework methods from the examples and the new Framework examples and I can extract the main top layer (Id, Name, slug ect) but I can’t seem to get to the sub arrays such as related ids, categories, tags etc as they are either presented as Objects (which I can’t convert to arrays - don’t know how) which are also sometimes empty or I can’t get to the child nodes in the Json data.

I have been at it for so long that I have lost track of what I did at the beginning and I will be rewriting it from scratch!

Any pointers, hints tips would be appreciated.

an example from my testing website:

[{ "id": 35, "name": "A DooDah (Copy) (Copy)", "slug": "a-doodah-copy-copy", "permalink": "https://www.greendealbromley.co.uk/product/a-doodah-copy-copy/", "date_created": "2016-07-27T08:55:51", "date_modified": "2016-07-27T13:56:44", "type": "simple", "status": "publish", "featured": false, "catalog_visibility": "visible", "description": "<p>This is a widget text</p>\ ", "short_description": "", "sku": "", "price": "29.54", "regular_price": "29.54", "sale_price": "", "date_on_sale_from": "", "date_on_sale_to": "", "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&pound;</span>29.54</span>", "on_sale": false, "purchasable": true, "total_sales": 0, "virtual": false, "downloadable": false, "downloads": [], "download_limit": -1, "download_expiry": -1, "download_type": "standard", "external_url": "", "button_text": "", "tax_status": "taxable", "tax_class": "", "manage_stock": true, "stock_quantity": 2, "in_stock": true, "backorders": "no", "backorders_allowed": false, "backordered": false, "sold_individually": false, "weight": "", "dimensions": { "length": "1", "width": "2", "height": "3" }, "shipping_required": true, "shipping_taxable": true, "shipping_class": "", "shipping_class_id": 0, "reviews_allowed": true, "average_rating": "0.00", "rating_count": 0, "related_ids": [16, 17, 9, 22, 21], "upsell_ids": [], "cross_sell_ids": [], "parent_id": 0, "purchase_note": "", "categories": [{ "id": 9, "name": "Bits", "slug": "bits" }], "tags": [{ "id": 7, "name": "Bits and Bobs", "slug": "bits-and-bobs" }], "images": [{ "id": 10, "date_created": "2016-04-21T16:31:43", "date_modified": "2016-04-21T16:31:43", "src": "https://www.greendealbromley.co.uk/wp-content/uploads/2016/04/Aquabion.jpg", "name": "Aquabion", "alt": "", "position": 0 }], "attributes": [], "default_attributes": [], "variations": [], "grouped_products": [], "menu_order": 0, "_links": { "self": [{ "href": "https://www.greendealbromley.co.uk/wp-json/wc/v1/products/35" }], "collection": [{ "href": "https://www.greendealbromley.co.uk/wp-json/wc/v1/products" }] } }, { "id": 34, "name": "My Widget (Copy) (Copy)", "slug": "my-widget-copy-copy", "permalink": "https://www.greendealbromley.co.uk/product/my-widget-copy-copy/", "date_created": "2016-07-27T08:55:45", "date_modified": "2016-07-27T08:55:47", "type": "simple", "status": "publish", "featured": false, "catalog_visibility": "visible", "description": "<p>This is a widget text</p>\ ", "short_description": "", "sku": "", "price": "29.54", "regular_price": "29.54", "sale_price": "", "date_on_sale_from": "", "date_on_sale_to": "", "price_html": "<span class=\"woocommerce-Price-amount amount\"><span class=\"woocommerce-Price-currencySymbol\">&pound;</span>29.54</span>", "on_sale": false, "purchasable": true, "total_sales": 0, "virtual": false, "downloadable": false, "downloads": [], "download_limit": -1, "download_expiry": -1, "download_type": "standard", "external_url": "", "button_text": "", "tax_status": "taxable", "tax_class": "", "manage_stock": true, "stock_quantity": 2, "in_stock": true, "backorders": "no", "backorders_allowed": false, "backordered": false, "sold_individually": false, "weight": "", "dimensions": { "length": "", "width": "", "height": "" }, "shipping_required": true, "shipping_taxable": true, "shipping_class": "", "shipping_class_id": 0, "reviews_allowed": true, "average_rating": "0.00", "rating_count": 0, "related_ids": [13, 16, 15, 19, 23], "upsell_ids": [], "cross_sell_ids": [], "parent_id": 0, "purchase_note": "", "categories": [{ "id": 6, "name": "Things", "slug": "things" }], "tags": [{ "id": 7, "name": "Bits and Bobs", "slug": "bits-and-bobs" }], "images": [{ "id": 10, "date_created": "2016-04-21T16:31:43", "date_modified": "2016-04-21T16:31:43", "src": "https://www.greendealbromley.co.uk/wp-content/uploads/2016/04/Aquabion.jpg", "name": "Aquabion", "alt": "", "position": 0 }], "attributes": [], "default_attributes": [], "variations": [], "grouped_products": [], "menu_order": 0, "_links": { "self": [{ "href": "https://www.greendealbromley.co.uk/wp-json/wc/v1/products/34" }], "collection": [{ "href": "https://www.greendealbromley.co.uk/wp-json/wc/v1/products" }] } }]

Using the new framework you can basically alternate between an array of type auto and dictionary to work down to what you’re looking for. In this (very) rough example the text variable jsontext contains your text above. It’s looking for the items in the first category array for id # 35. It doesn’t do important checking like seeing if a dictionary key exists or if the array is not empty:

[code]dim slug, name as text
dim d, e as xojo.Core.Dictionary
dim a(), b() as auto
dim id as integer

a = xojo.data.ParseJSON(jsontext)

for each d in a

if  d.Value("id") = 35 then
  
  b = d.Value("categories")
  
  e = b(0)
  
  id = e.Value("id")
  slug = e.Value("slug")
  name = e.value("name")
  
end if

next[/code]

Hi Scott, thanks for the example.

I have been testing some more and it has helped me work a way around my issue but is there a way of creating a dictionary from the contents of a sub array e.g. the “images” section, like the parent JSON file is an array containing a dictionary? That way I can loop through the dictionary to add the contents to a database (like I do with the parent) rather than hard coding each entry?

Cheers,

Paul

In my example b is an array of dictionaries. Just loop through b:

[code]for each e in b
if e.haskey(“id”) then
’ do something with e.value(“id”)
end if

if e.haskey(“slug”) then
’ do something with e.value(“slug”)
end if

if e.haskey(“name”) then
’ do something with e.value(“name”)
end if
next

[/code]

Hi Scott,

Finally got some time to get back and work on this and thanks to your snippets I have managed to get the whole lot into my DB properly.

Thanks,

Paul

@Paul Stevenson : Do you have an example on how to do authentication from Xojo to Woocommerce? I try to figure out how I could do this from a Xojo desktop app, with version 3.0.6 of woocommerce (using its current REST API v2).

I’m trying to use a Xojo.Net.HTTPSocket and set authorization data in the initial header (with Socket.RequestHeader).

If you have done such a thing already, then would you be ready to share an example with us?
Thanks!

Hi Oliver,

Is your target site using https?

I cheated and made sure my e-commerce site used Https which made everything much simpler.

I also used MonkeyBread Software plugin CURLSMBS to handle the connection and JSON stuff.

Paul

Yes, website enforces https.

Is it possible to share here, or via pm, a codesnippet which shows how you initiate the connection to woocommerce? (I can use MBS Plugins as well, if necessary or easier).

But don’t worry, if you feel in any way uncomfortable with my request, then I will figure it out myself (just takes more try and error… :slight_smile:

Hi Oliver,

No problem however you may still need a bit of playing as the project I was working on was shelved before completion.

But, I do have my little test app the see what happens with various Get, Put etc commands.

You will need to add your full web address (my testing address is still there as an example), consumer and secret keys to the shared properties in addition to checking the API url (I was testing WP API v1 but it is now v2). You will also need some MBS plugins for the CURLSMBS.

Here is the link to the application -

https://www.dropbox.com/s/l28922fe60qa96v/TestWooCommerce.xojo_binary_project?dl=0

Note - This was a play thing for testing so it is ugly, incomplete and some things might already be broken. From memory GET works, the rest is possible but might help you reduce the trial and error :slight_smile:

Hope that helps

@Paul Stevenson : Thanks a lot, Paul! This helps a lot! Your generosity definitely saves me some time. Now I owe you one :slight_smile: