Understanding VBJSON class set up in Excel 2010 - json

I have downloaded and set the JSON parser for VB6 from this website:
VB-JSON
I cannot understand how this module works. I successfuly set it in Excel 2010 and I think I also understand the JSON format but I don't know how this class extracts the items. I tried to parsed the following text in the JSON format
{"realms":[{"type":"pvp","population":"low","queue":false,"wintergrasp":{"area":1,"controlling-faction":1,"status":0,"next":1356724174636},"tol-barad":{"area":21,"controlling-faction":1,"status":0,"next":1356723246779},"status":true,"name":"Kor'gall","slug":"korgall","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"},{"type":"pve","population":"medium","queue":false,"wintergrasp":{"area":1,"controlling-faction":0,"status":0,"next":1356724425638},"tol-barad":{"area":21,"controlling-faction":0,"status":0,"next":1356723369780},"status":true,"name":"Alonsus","slug":"alonsus","battlegroup":"Cruelty / Crueldad","locale":"en_GB","timezone":"Europe/Paris"}]}
A browser will return this content when the following address is used:
Alonsus, Kor'gall
I wrote a code similar to this
Dim objJSON As Object
...
strData = objJSON.Item("Realms")(1).Item("Type")
but it causes errors: "Object variable or With block variable not set". I expected to get "pve" value. I am confused because it starts with an object name "realms" followed by an Array. The array is supposed to be returned as a collection. I would be grateful for any help with that.
Thanks

set objJSON = JSON.Parse(jsonFromUrl)
strData = objJSON.Item("realms").Item(1).Item("type")
Keys inside json is case-sensitive.

Related

Newtonsoft.Json.JsonSerializationException - Cannot deserialize the current JSON object

Lot of similar questions but still not able to make it. Here is my
code in vb.net and this is the json response. enter image description here I know this is because of [] but i'm using list don't know still getting the same error.
This is because you are receiving a JSON array and not a single JSON object.
You need to deserialize the array like this -
Dim Items_Array = Newtonsoft.Json.JsonConvert.DeserializeObject(Of T())(jsonString)

How to get data from JSON nested arrays in excel vba-json

I'm preparing some kind of software working with Excel with an API, and I'm using vba-json library, and I dont know how to get data from nested arrays on JSON.
I've tryied some tutorials and another similar questions that I fond here. But every time I try I get a different error. Runtime error 5, 9, 13. Tryied different ways to accés the data but everytime I get error when I get into an array.
Set MyRequest = CreateObject("WinHttp.WinHttpRequest.5.1")
MyRequest.Open "GET", "https://pokeapi.co/api/v2/pokemon/ditto/?fbclid=IwAR2pJwAgODOlI-Gdn8pH-RDFCcUfQWiYLZIVCnP8e-V_9gEwYymqRldpiFk"
MyRequest.Send
Dim Json As Object
Set Json = JsonConverter.ParseJson(MyRequest.ResponseText)
MsgBox Json("stats")("id")(0)("base_stat")
I'd like to get the data from the selected array to later be able to work with it, for example from
("stats")("id")(0)("base_stat")
get
48
The Json source is on the code.
So, with JSON the [] denotes a collection you can For Each over and access by index, the {} indicates dictionaries you can For Each the dict keys of, or access items by key.
You can get a feel for the structure by pasting it into a json viewer such as this. You will need to familiarise yourself with reading json but you will see that, for that value you want, it has a path of:
json("stats")(1)("base_stat")
Note: The indexing starts at one for collections in VBA JSON though it is displays 0 in the viewer.
Reading the structure:
Option Explicit
Public Sub test()
Dim Json As Object
With CreateObject("WinHttp.WinHttpRequest.5.1")
.Open "GET", "https://pokeapi.co/api/v2/pokemon/ditto/?fbclid=IwAR2pJwAgODOlI-Gdn8pH-RDFCcUfQWiYLZIVCnP8e-V_9gEwYymqRldpiFk"
.send
Set Json = JsonConverter.ParseJson(.responseText)
End With
MsgBox Json("stats")(1)("base_stat")
End Sub
Note there are various answers on StackOverflow which provide code that will list the access paths to every item in a json structure. In particular, I remember this great answer from #tinman here. It's not a substitute for learning to read JSON but an excellent tool for aiding and checking your understanding.

Read JSON file vb.net

Good day,
I need to get some values from a JSON file, but I can not deserialize the file.
It is available here:http://files.minecraftforge.net/maven/net/minecraftforge/forge/json
What I would like to get are the values of "mcversion" and "version" found in the "number" section, the remaining data not interest me.
I generated classes but do not work due to JSON format, this special is different.
I hope you can help me, thank you very much.
To Read from a File you can use
Dim json As String = File.ReadAllText("JSON File Path")
Dim o As JObject = JObject.Parse(json)

VBA getting values from a collection?

I am very new to VBA and I can not figure out how to get values from a Collection.
This is my code:
Dim p As Object
Set p = JSON.parse(Response.Content)
Dim links As Object
Set links = p.Item("links")
In the debugger for "links" I see:
I am using this library to parse json : http://www.ediy.co.nz/vbjson-json-parser-library-in-vb6-xidc55680.html
The part I have in json is:
"links":[
{
"rel":"next",
"href":"www.google.com"
}
]
How can I get the value of "rel" here?
Don't forget the bang operator, designed for collection access by key:
links(1)!rel
or:
links(1)![rel] 'When there are spaces or reserved words.
I will answer my own question:
links(1).Item("rel")
worked...
Regards..
Using JavaScript features of parsing JSON, on top of ScriptControl, we can create a parser in VBA which will list each and every data point inside the JSON. No matter how nested or complex the data structure is, as long as we provide a valid JSON, this parser will return a complete tree structure.
JavaScript’s Eval, getKeys and getProperty methods provide building blocks for validating and reading JSON.
Coupled with a recursive function in VBA we can iterate through all the keys (up to nth level) in a JSON string. Then using a Tree control (used in this article) or a dictionary or even on a simple worksheet, we can arrange the JSON data as required.
You can see the full VBA code here

Use ScriptControl to parse JSON in VBA: transform result to dictionaries and collections

I would like to use Microsoft ScriptControl to parse a JSON string in VBA, and then transform the resulting Object into Dictionary and Collection objects. I already know how to do the parsing with ScriptControl, but cannot figure out how to map the result into the Dictionary and Collection classes. I'm guessing that if I could figure out how to loop through the properties of an Object this would become clear...
Dim sc As ScriptControl
Dim obj As Variant
Set sc = CreateObject("ScriptControl")
sc.Language = "JScript"
Set obj = sc.Eval("("+json+")") ' json is a string containing raw JSON
' Now what?
By the way, I've used the vba-json library to get the output in terms of Dictionaries and Collections, but I find this library somewhat slow. It does not use ScriptControl.
EDIT: I found a discussion of getting object properties in this post.
Using JavaScript features of parsing JSON, on top of ScriptControl, we can create a parser in VBA which will list each and every data point inside the JSON. No matter how nested or complex the data structure is, as long as we provide a valid JSON, this parser will return a complete tree structure.
JavaScript’s Eval, getKeys and getProperty methods provide building blocks for validating and reading JSON.
Coupled with a recursive function in VBA we can iterate through all the keys (up to nth level) in a JSON string. Then using a Tree control (used in this article) or a dictionary or even on a simple worksheet, we can arrange the JSON data as required.
VBA Code:http://ashuvba.blogspot.in/2014/09/json-parser-in-vba-browsing-through-net.html
loop
This will help you to loop - add a myitem(n) method in javascript
from there you can map through VB code.