Have written code to call API using VBA from MS Access, but having issues extracting nested data. Am able to retrieve data from Items set, but also want to retrieve data from Attributes set where SetName=DG (in the below example there is only 1 AttributeSet, but could be more). Have followed some of the other similar posts, but just not working. Returned Data:
{"Pagination":
{"NumberOfItems":1,
"PageSize":200,
"PageNumber":1,
"NumberOfPages":1},
"Items":[
{"ProductCode":"TEST",
"ProductDescription":"TEST",
"UnitOfMeasure":
{"Guid":"7e420466-4ced-48df-bb41-1693fe34a32c",
"Name":"EA",
"Obsolete":false},
"NeverDiminishing":false,
"ImageUrl":null,
"SellPriceTier1":
{"Name":"Sell Price Tier 1",
"Value":null},
"SellPriceTier2":
{"Name":"Sell Price Tier 2",
"Value":null},
"Supplier":null,
"AttributeSet":
{"Guid":"c3bd26c9-424a-4786-adbe-7c5a98b8f422",
"SetName":"DG",
"Type":"Product",
"Attributes":[
{"Guid":"6164f12b-2cb9-491c-b932-e6fb050579df",
"Name":"UN",
"Value":"1993",
"IsRequired":false},
{"Guid":"aa13f1dd-2174-4993-b80d-22bf4f4f27da",
"Name":"Technical Name",
"Value":"2K REDUCED",
"IsRequired":false},
{"Guid":"664fbcd6-83be-4afc-b812-22c97ae38949",
"Name":"Flash Point",
"Value":"30",
"IsRequired":false},
{"Guid":"3bc41b7c-bd14-44f6-a6b0-72d1ba84adbb",
"Name":"Pack Group",
"Value":"III",
"IsRequired":false}]},
"IsSellable":true,
"AlternateUnitsOfMeasure":[
{"Guid":"d42f5682-02b3-43fa-a848-46e6023c3b9e",
"Name":"LT",
"ConversionRate":1.0000,
"ForPurchases":true}],
"LastModifiedOn":"\/Date(1674949964652)\/"}
]
}
Code:
Dim key_id, secret_key, URL As String
Dim strJson As String
Dim req As New XMLHTTP60
Dim strModule As String
Dim strFilter As String
Dim rs, rs1 As DAO.Recordset
Dim JsonText As Object
Dim Item As Object
Dim attset As Object
Dim att As Object
key_id = "API ID"
secret_key = "API key"
URL = "https://api.unleashedsoftware.com/"
strModule = "Products?"
strFilter = "productCode=TEST&includeAttributes=True"
strJson = URL + strModule + strFilter
req.Open "GET", strJson, False
req.setRequestHeader "api-auth-id", key_id
req.setRequestHeader "api-auth-signature", Base64HMAC("SHA256", strFilter, secret_key)
req.setRequestHeader "Content-Type", "application/json"
req.setRequestHeader "Accept", "application/json"
req.sEnd
Set JsonText = JsonConverter.ParseJson(req.responseText)
For Each Item In JsonText("Items")
Set rs = CurrentDb.OpenRecordset("aProduct")
With rs
.AddNew
!prodcode = Item("ProductCode")
!proddesc = Item("ProdutDescription")
.Update
End With
For Each att In Item("Attributes")
'get Attribute Value
Set rs1 = CurrentDb.OpenRecordset("aProdDGAttribute")
With rs1
.AddNew
!Name = att("Name")
!Value= att("Value")
.Update
End With
Next att
Next Item
MsgBox "done"
locals window screenshot:

JsonTextto get a sense of how it's structured