Parsing JSon in Classic ASP with ASP Xtreme Evolution - json

I'm working on an classic ASP-project and I use ASP Xtreme Evolution to parse JSon data (found here: http://zend.lojcomm.com.br/entries/classic-asp-json-revisited/)
Anyway... I'm getting most of the JSon-data correctly but now I'm stuck on an Array.
The JSon looks like this:
{
"Product": {
"ID": "6666",
"Name": "Tha name",
"ArticleGroup": [
{
"#handleas": "array",
"Title": "Title 1",
"Article": {
"ID": "777",
"Label": "Label 1",
}
},
{
"#handleas": "array",
"Title": "Title 2",
"Article": {
"ID": "888",
"Label": "Label 2",
}
}
]
}
}
}
And the ASP look like this:
set xmlHTTP = server.createobject("MSXML2.ServerXMLHTTP.6.0")
xmlHTTP.open "GET", "http://source.of.json", false
xmlHTTP.send()
ProductFeed = xmlHTTP.ResponseText
dim ProductInfo : set ProductInfo = JSON.parse(join(array(ProductFeed)))
dim key : for each key in ProductInfo.Product.keys()
Response.Write ProductInfo.Product.ID ' Prints 6666
Response.Write ProductInfo.Product.Name ' Prints Tha Name
Next
set ProductInfo = nothing
My problem is that I cannot figure out how to access the information in ArticleGroup. All I get is [object Object],[object Object] or an empty value.
Anyone have any ideas?
Thanx!

Try the following (based on the given JSON):
Dim ArticleGroup
dim key : For Each key in ProductInfo.Product.keys()
Response.Write ProductInfo.Product.ID ' Prints 6666
Response.Write ProductInfo.Product.Name ' Prints Tha Name
For Each ArticleGroup In ProductInfo.Product.Get("ArticleGroup") '' iterate all ArticleGroup entries
Response.write ArticleGroup.Get("Title") '' get the correct key value
Next
Next
You need to iterate all ArticleGroup entries and get the value through .get(keyName). Otherwise it will return the JScript function body instead.

Related

Parse multiple JSON/VBA objects with VBA-JSON

I have the following JSON response:
[
{
"id": 1354345345,
"date": "2021-10-01T23:29:42.000000+02:00",
"count": 0,
"year": 2020,
"area": 232,
"numberList": [
8693978
],
"Property": {
"PropertyID": 135005860000118,
"PropertyNumber": 2244
}
},
{
"id": 2345235345,
"date": "2021-02-13T13:40:30.000000+01:00",
"count": 2,
"year": 2020,
"area": 122,
"numberList": [
8693978
],
"Property": {
"PropertyID": 153005860001536,
"PropertyNumber": 1555
}
}
]
Inside the array [], there can be several object {}, each with a value in "area". In this example, there are two objects in the array.
I am trying to parse the JSON via VBA-JSON.
I've tried the following VBA code, but it will only return the names of each object item and not the value.
Set Json = JsonConverter.ParseJson(responseText)
For Each Item In Json
For Each i In Item
Debug.Print i
Next
Next
VBA debug console will show:
id
date
count
year
area
numberList
Property
id
date
count
year
area
numberList
Property
How do I fetch the area of each object?
JsonConverter.ParseJson(responseText) creates an object keeping Scripting Dictionaries... So, they expose keys and items. Based on that logic, please try the next way of "area" values extracting:
The string you show misses a ":" character (replaced with ";").Here: "area"; 232,. It should be correct to be parsed...
The next code version will iterate between the object keys and items, extracted the value (item) for the key "area":
Dim json As Object, strFile As String, responseText As String, dict, i As Long
responseText = "the corrected string you show..."
Set json = JsonConverter.ParseJSON(responseText)
For Each dict In json
For i = 0 To dict.count - 1
If dict.Keys()(i) = "area" Then
Debug.Print TypeName(dict), TypeName(dict.Keys()(i)), TypeName(dict.Items()(i))
Debug.Print dict.Items()(i)
End If
Next i
Next dict

Access nested values in JSON-Object in VBA

I would like to get data from a JSON-Object, that I got from a Rest-API, with VBA to display some data into an Excel-Worksheet. I'm using the library (VBA-JSON v2.3.1 JsonConverter).
I have the following JSON-Object:
{
"devices": [
{
"data": [
{
"id": 0,
"name": "Hello"
},
{
"id": 1,
"name": "How are you?"
},
{
"id": 2,
"name": "Bye"
}
],
"type": "LORA"
}
],
"includedTypes": [
"LORA"
]
}
I want to get the objects in the array from "data".
My VBA-Code is this:
Dim js1Object As Object
Dim response1 As String
strUrl = "https://XXXXXXXXXXXdevices
Set hReq = CreateObject("MSXML2.XMLHTTP")
With hReq
.Open "GET", strUrl, False
.SetRequestHeader "Authorization", "Bearer " & apitoken
.Send
response1 = hReq.responseText
MsgBox response1
Set js1Object = JsonConverter.ParseJson(response1)
j = 31
For Each item In js1Object("devices")
ws.Cells(j, 7) = item("id")
ws.Cells(j, 10) = item("name")
j = j + 1
Next
MsgBox (response1)
End With
How can I access the values from "data"?
If the JSON would look like the object below, my code would work. But my problem is, that the response that I get, is more nested and I can't directly access "data".
{
"devices": [
{
"id": 0,
"name": "Hello"
},
{
"id": 1,
"name": "How are you?"
},
{
"id": 2,
"name": "Bye"
}
]
}
I just don't know, how to access deeper values in JSON-Object. The solutions from similar questions with print are not working with my code.
Thanks for helping me!
Your "root" json object is a Dictionary - the key "devices" is a Collection object, and the first element is another dictionary with two keys "data" and "type".
"data" is another Collection of Dictionaries, so you can do this to get to the contained id and name values:
Dim Json As Object, data, d
'reading json from a worksheet cell...
Set Json = JsonConverter.ParseJson(Range("A5").Value)
Set data = Json("devices")(1)("data") 'Dictionary key->Collection index->Dictionary key
For Each d In data
Debug.Print d("id"), d("name")
Next d
Output:
0 Hello
1 How are you?
2 Bye

Ruby: How to parse json to specific types

I have a JSON that I want to parse in Ruby. Ruby is completely new to me, but I have to work with it :-)
Here is my litte snippet, that should do the parsing:
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response)
this works pretty fine. The only downside is, I do not know the properties at the point where I use it, it is not typesafe. So I created the objects for it
class Announcements
##announcements = Hash # a map key => value where key is string and value is type of Announcement
end
class Announcement
##name = ""
##status = ""
##rewards = Array
end
And this is how the json looks like
{
"announcements": {
"id1" : {
"name": "The Diamond Announcement",
"status": "published",
"reward": [
{
"id": "hardCurrency",
"amount": 100
}
]
},
"id2": {
"name": "The Normal Announcement",
"players": [],
"status": "published",
"reward": []
}
}
}
So I tried JSON parsing like this
response = File.read("app/helpers/example_announcement.json")
JSON.parse(response, Announcements)
But this is not how it works^^can anybody help me with this?

VbScript Deserialize JSON

Using the "ASPJSON" library - http://www.aspjson.com/
I am attempting to get to the "emailAddress" value in the following JSON string
{
"result": {
"lead": [
{
"id": "1",
"emailAddress": "mickey#mouse.com"
},
{
"id": "2",
"emailAddress": "mini#mouse.com"
}
]
},
"error": null,
"id": "1"
}
I am having problems getting to the value using loops, which I believe is the only way to pul out the 2 email addresses.
This is the code I have so far:
For Each result In oJSON.data("result")
Set this = oJSON.data("result")
For Each lead in this("lead")
Set this2 = oJSON.data("active").item(lead)
response.Write("lead") & "<br>"
Next
Next
When I commend out the line
Set this2 = oJSON.data("active").item(lead)
I get 2 lines written by the response.Write so I believe I have got down to the right "level"
But I have failed trying to pull out the values.
The example given by the author of the library looks like this:
But I have been unable to get it to do what I need:
The example JSON:
{
"firstName": "John",
"lastName" : "Smith",
"age" : 25,
"address" :
{
"streetAddress": "21 2nd Street",
"city" : "New York",
"state" : "NY",
"postalCode" : "10021"
},
"phoneNumber":
[
{
"type" : "home",
"number": "212 555-1234"
},
{
"type" : "fax",
"number": "646 555-4567"
}
]
}
And the code example is:
<!--#include virtual="/aspJSON1.17.asp" -->
<%
Set oJSON = New aspJSON
'Load JSON string
oJSON.loadJSON(jsonstring)
'Get single value
Response.Write oJSON.data("firstName") & "<br>"
'Loop through collection
For Each phonenr In oJSON.data("phoneNumber")
Set this = oJSON.data("phoneNumber").item(phonenr)
Response.Write _
this.item("type") & ": " & _
this.item("number") & "<br>"
Next
'Update/Add value
oJSON.data("firstName") = "James"
'Return json string
Response.Write oJSON.JSONoutput()
%>
Finally got it working!
For Each result In oJSON.data("result")("lead")
Set this = oJSON.data("result")("lead").item(result)
response.Write this.item("emailAddress") & "<br>"
Next

Iterating through nested objects with VBJSON

I am attempting to connect to the SmartSheet API through VBA to pull the contents into an Excel sheet. I found the VBJSON library which has helped me a bit but I am struggling with iterating through the objects and pulling specific values.
I want to access the contents of the "Value" attribute for each row then do the same for subsequent rows. My biggest problem is that I do not know how this VBJSON library works since I cannot find any documentation on it and there are only a few examples and they deal with relatively straightforward JSON examples.
Desired Output
Row 1 Column 1 Content | Row 1 Column 2 Content
Row 2 Column 1 Content | Row 2 Column 2 Content
JSON
{
"id": 1,
"name": "Sheet Name",
"columns": [
{
"id": 1,
"index": 0,
"title": "Title of Column",
"type": "TEXT_NUMBER",
"primary": true
},
{
"id": 2,
"index": 1,
"title": "Title of Second Column",
"type": "TEXT_NUMBER"
},
],
"rows": [
{
"id": 1,
"rowNumber": 1,
"cells": [
{
"type": "TEXT_NUMBER",
"value": "Row 1 Column 1 Content",
"columnId": 1,
},
{
"type": "TEXT_NUMBER",
"value": "Row 1 Column 2 Content",
"columnId": 2,
},
],
"locked": true,
"lockedForUser": true,
"expanded": true,
"createdAt": "2013-10-11T13:43:24-05:00",
"modifiedAt": "2013-11-12T15:13:54-06:00"
},
{
"id": 2276445193037700,
"rowNumber": 2,
"cells": [
{
"type": "TEXT_NUMBER",
"value": "row 2 column 1 content",
"columnId": 1,
},
{
"type": "TEXT_NUMBER",
"value": "row 2 column 2 content",
"columnId": 2,
}
]
}
VBJSON library
http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html
Below is code I've pieced together from what I could find online and right now it pulls the values associated with each attribute in the row. But I only need to pull the contents of the "Value" portion and I can't seem to figure out how to do that. I think I really just need help with my for loop because I have the JSON, I have a library that appears to work, I am just struggling to figure out how to combine it all.
Dim xmlHttp As Object
Set xmlHttp = CreateObject("MSXML2.ServerXMLHTTP.6.0")
xmlHttp.Open "GET", URl, False
xmlHttp.setRequestHeader "Content-Type", "text/xml"
xmlHttp.send
Dim strDiv As String, startVal As Long, endVal As Long
strDiv = xmlHttp.ResponseText
startVal = InStr(1, strDiv, "rows", vbTextCompare)
endVal = InStr(startVal, strDiv, "]", vbTextCompare)
strDiv = "{" & Mid(strDiv, startVal - 1, (endVal - startVal) + 2) & "}"
Dim JSON As New JSON
Dim p As Object
Set p = JSON.parse(strDiv)
i = 1
For Each Item In p("rows")(1)("cells")(1)
Cells(2, i) = p("rows")(1)("cells")(1)(Item)
i = i + 1
Next
Ran into a similar problem, see my answer here: https://stackoverflow.com/a/16825736/1240745
This library has been a life-saver for me: https://github.com/VBA-tools/VBA-JSON (previously https://code.google.com/p/vba-json/)
I use it in a library I wrote for accessing Salesforce, Trello, and a few others. (Shameless plug): https://github.com/VBA-tools/VBA-Web
Using the VBA-JSON library, it would involve something like the following:
Dim Parsed As Dictionary
Set Parsed = JsonConverter.ParseJson(xmlHttp.ResponseText)
' Object -> Dictionary, so Row and Cell: Dictionary
Dim Row As Dictionary
Dim Cell As Dictionary
' Array -> Collection, so Parsed("rows"): Collection
For Each Row In Parsed("rows")
For Each Cell In Row("cells")
' Access Dictionary items by key
Cells(Row("rowNumber"), Cell("columnId")) = Cell("value")
Next Cell
Next Row
(or something similar)