how to extract elements in json string in vba - json

I have the following json string:
[
{
"Features": {
"Level": [
{
"endDate": "2018-12-11",
"minimum": "0.000000000000",
"maximum": "0.000000000000",
"value": "228.108000000000",
"payDate": "0"
},
{
"endDate": "2018-12-11",
"minimum": "0.000000000000",
"maximum": "0.000000000000",
"value": "3143.513000000000",
"payDate": "0"
}
]
}
},
]
I was trying to extract the two values in excel using vba and have the following code (where result2 would be the json string)
Public Sub GetS (result2 As String, m As Integer)
Dim activeWS As Worksheet
Set activeWS = ThisWorkbook.Worksheets("Data")
Dim jsonStr As String, json As Object, headers()
jsonStr = result2
Set json = JsonConverter.ParseJson(jsonStr)(1)
activeWS.Cells(m, 19) = json("Features")("Level")(0)("value")
activeWS.Cells(m, 20) = json("Features")("Level")(1)("value")
End Sub
the second part of the vba works, where it grab the value of 3143.51 (the second number), i'm wondering how can I get the first value (228.10).
I've tried using ("Initial Level")(0)("value") but it doesn't work.
Thank you so much.

JSON arrays get converted to VBA Collections - these are indexed from 1 not from zero
Try this:
jsonStr = Range("I10").Value 'your json sample
Set json = JsonConverter.ParseJson(jsonStr)(1)
Debug.Print json("Features")("Level")(1)("value") '>>228.108000000000
Debug.Print json("Features")("Level")(2)("value") '>>3143.513000000000
(tested and confirmed)

Here is how to access data in the JSON string:
Json("match")("awayTeam")("coach")("countryOfBirth")
Json("match")("awayTeam")("coach")("nationality")
Json("match")("awayTeam")("captain")("id")
Json("match")("awayTeam")("captain")("name")
Json("match")("awayTeam")("captain")("shirtNumber")
Json("match")("awayTeam")("lineup")(1)("id")
Json("match")("awayTeam")("lineup")(1)("name")
Json("match")("awayTeam")("lineup")(1)("position")
Json("match")("awayTeam")("lineup")(1)("shirtNumber")
Json("match")("awayTeam")("lineup")(2)("id")
Json("match")("awayTeam")("lineup")(2)("name")
Json("match")("awayTeam")("lineup")(2)("position")
Json("match")("awayTeam")("lineup")(2)("shirtNumber")
Json("match")("awayTeam")("lineup")(3)("id")
Json("match")("awayTeam")("lineup")(3)("name")
Json("match")("awayTeam")("lineup")(3)("position")
Json("match")("awayTeam")("lineup")(3)("shirtNumber")
Json("match")("awayTeam")("lineup")(4)("id")
Json("match")("awayTeam")("lineup")(4)("name")
Json("match")("awayTeam")("lineup")(4)("position")
Json("match")("awayTeam")("lineup")(4)("shirtNumber")
Json("match")("awayTeam")("lineup")(5)("id")
Json("match")("awayTeam")("lineup")(5)("name")
Json("match")("awayTeam")("lineup")(5)("position")
Json("match")("awayTeam")("lineup")(5)("shirtNumber")
Json("match")("awayTeam")("lineup")(6)("id")
Json("match")("awayTeam")("lineup")(6)("name")
Json("match")("awayTeam")("lineup")(6)("position")
Json("match")("awayTeam")("lineup")(6)("shirtNumber")
Json("match")("awayTeam")("lineup")(7)("id")
Json("match")("awayTeam")("lineup")(7)("name")
Json("match")("awayTeam")("lineup")(7)("position")
Json("match")("awayTeam")("lineup")(7)("shirtNumber")
Json("match")("awayTeam")("lineup")(8)("id")
Json("match")("awayTeam")("lineup")(8)("name")
Json("match")("awayTeam")("lineup")(8)("position")
Json("match")("awayTeam")("lineup")(8)("shirtNumber")
Json("match")("awayTeam")("lineup")(9)("id")
Json("match")("awayTeam")("lineup")(9)("name")
Json("match")("awayTeam")("lineup")(9)("position")
Json("match")("awayTeam")("lineup")(9)("shirtNumber")
Json("match")("awayTeam")("lineup")(10)("id")
Json("match")("awayTeam")("lineup")(10)("name")
Json("match")("awayTeam")("lineup")(10)("position")
Json("match")("awayTeam")("lineup")(10)("shirtNumber")
Json("match")("awayTeam")("lineup")(11)("id")
Json("match")("awayTeam")("lineup")(11)("name")
Json("match")("awayTeam")("lineup")(11)("position")
Json("match")("awayTeam")("lineup")(11)("shirtNumber")
Json("match")("awayTeam")("bench")(1)("id")
Json("match")("awayTeam")("bench")(1)("name")
Json("match")("awayTeam")("bench")(1)("position")
Json("match")("awayTeam")("bench")(1)("shirtNumber")
Json("match")("awayTeam")("bench")(2)("id")
Json("match")("awayTeam")("bench")(2)("name")
Json("match")("awayTeam")("bench")(2)("position")
Json("match")("awayTeam")("bench")(2)("shirtNumber")
Json("match")("awayTeam")("bench")(3)("id")
Json("match")("awayTeam")("bench")(3)("name")
Json("match")("awayTeam")("bench")(3)("position")
Json("match")("awayTeam")("bench")(3)("shirtNumber")
Json("match")("awayTeam")("bench")(4)("id")
Json("match")("awayTeam")("bench")(4)("name")
Json("match")("awayTeam")("bench")(4)("position")
Json("match")("awayTeam")("bench")(4)("shirtNumber")
Json("match")("awayTeam")("bench")(5)("id")
Json("match")("awayTeam")("bench")(5)("name")
Json("match")("awayTeam")("bench")(5)("position")
Json("match")("awayTeam")("bench")(5)("shirtNumber")
Json("match")("awayTeam")("bench")(6)("id")
Json("match")("awayTeam")("bench")(6)("name")
Json("match")("awayTeam")("bench")(6)("position")
Json("match")("awayTeam")("bench")(6)("shirtNumber")
Json("match")("awayTeam")("bench")(7)("id")
Json("match")("awayTeam")("bench")(7)("name")
Json("match")("awayTeam")("bench")(7)("position")
Json("match")("awayTeam")("bench")(7)("shirtNumber")
Json("match")("goals")(1)("minute")
Json("match")("goals")(1)("extraTime")
Json("match")("goals")(1)("type")
Json("match")("goals")(1)("team")("id")
Json("match")("goals")(1)("team")("name")
Json("match")("goals")(1)("scorer")("id")
Json("match")("goals")(1)("scorer")("name")
Json("match")("goals")(1)("assist")("id")
Json("match")("goals")(1)("assist")("name")
Json("match")("goals")(2)("minute")
Json("match")("goals")(2)("extraTime")
Json("match")("goals")(2)("type")
Json("match")("goals")(2)("team")("id")
Json("match")("goals")(2)("team")("name")
Json("match")("goals")(2)("scorer")("id")
Json("match")("goals")(2)("scorer")("name")
Json("match")("goals")(2)("assist")("id")
Json("match")("goals")(2)("assist")("name")
Json("match")("goals")(3)("minute")
Json("match")("goals")(3)("extraTime")
Json("match")("goals")(3)("type")
Json("match")("goals")(3)("team")("id")
Json("match")("goals")(3)("team")("name")
Json("match")("goals")(3)("scorer")("id")
Json("match")("goals")(3)("scorer")("name")
Json("match")("goals")(3)("assist")
Json("match")("bookings")(1)("minute")
Json("match")("bookings")(1)("team")("id")
Json("match")("bookings")(1)("team")("name")
Json("match")("bookings")(1)("player")("id")
Json("match")("bookings")(1)("player")("name")
Json("match")("bookings")(1)("card")
Json("match")("bookings")(2)("minute")
Json("match")("bookings")(2)("team")("id")
Json("match")("bookings")(2)("team")("name")
Json("match")("bookings")(2)("player")("id")
Json("match")("bookings")(2)("player")("name")
Json("match")("bookings")(2)("card")
Json("match")("bookings")(3)("minute")
Json("match")("bookings")(3)("team")("id")
Json("match")("bookings")(3)("team")("name")
Json("match")("bookings")(3)("player")("id")
Json("match")("bookings")(3)("player")("name")
Json("match")("bookings")(3)("card")
Json("match")("bookings")(4)("minute")
Json("match")("bookings")(4)("team")("id")
Json("match")("bookings")(4)("team")("name")
Json("match")("bookings")(4)("player")("id")
Json("match")("bookings")(4)("player")("name")
Json("match")("bookings")(4)("card")
Json("match")("bookings")(5)("minute")
Json("match")("bookings")(5)("team")("id")
Json("match")("bookings")(5)("team")("name")
Json("match")("bookings")(5)("player")("id")
Json("match")("bookings")(5)("player")("name")
Json("match")("bookings")(5)("card")
Json("match")("bookings")(6)("minute")
Json("match")("bookings")(6)("team")("id")
Json("match")("bookings")(6)("team")("name")
Json("match")("bookings")(6)("player")("id")
Json("match")("bookings")(6)("player")("name")
Json("match")("bookings")(6)("card")
Json("match")("bookings")(7)("minute")
Json("match")("bookings")(7)("team")("id")
Json("match")("bookings")(7)("team")("name")
Json("match")("bookings")(7)("player")("id")
Json("match")("bookings")(7)("player")("name")
Json("match")("bookings")(7)("card")
Json("match")("substitutions")(1)("minute")
Json("match")("substitutions")(1)("team")("id")
Json("match")("substitutions")(1)("team")("name")
Json("match")("substitutions")(1)("playerOut")("id")
Json("match")("substitutions")(1)("playerOut")("name")
Json("match")("substitutions")(1)("playerIn")("id")
Json("match")("substitutions")(1)("playerIn")("name")
Json("match")("substitutions")(2)("minute")
Json("match")("substitutions")(2)("team")("id")
Json("match")("substitutions")(2)("team")("name")
Json("match")("substitutions")(2)("playerOut")("id")
Json("match")("substitutions")(2)("playerOut")("name")
Json("match")("substitutions")(2)("playerIn")("id")
Json("match")("substitutions")(2)("playerIn")("name")
Json("match")("substitutions")(3)("minute")
Json("match")("substitutions")(3)("team")("id")
Json("match")("substitutions")(3)("team")("name")
Json("match")("substitutions")(3)("playerOut")("id")
Json("match")("substitutions")(3)("playerOut")("name")
Json("match")("substitutions")(3)("playerIn")("id")
Json("match")("substitutions")(3)("playerIn")("name")
Json("match")("substitutions")(4)("minute")
Json("match")("substitutions")(4)("team")("id")
Json("match")("substitutions")(4)("team")("name")
Json("match")("substitutions")(4)("playerOut")("id")
Json("match")("substitutions")(4)("playerOut")("name")
Json("match")("substitutions")(4)("playerIn")("id")
Json("match")("substitutions")(4)("playerIn")("name")
Json("match")("substitutions")(5)("minute")
Json("match")("substitutions")(5)("team")("id")
Json("match")("substitutions")(5)("team")("name")
Json("match")("substitutions")(5)("playerOut")("id")
Json("match")("substitutions")(5)("playerOut")("name")
Json("match")("substitutions")(5)("playerIn")("id")
Json("match")("substitutions")(5)("playerIn")("name")
Json("match")("substitutions")(6)("minute")
Json("match")("substitutions")(6)("team")("id")
Json("match")("substitutions")(6)("team")("name")
Json("match")("substitutions")(6)("playerOut")("id")
Json("match")("substitutions")(6)("playerOut")("name")
Json("match")("substitutions")(6)("playerIn")("id")
Json("match")("substitutions")(6)("playerIn")("name")
Json("match")("referees")(1)("id")
Json("match")("referees")(1)("name")
Json("match")("referees")(1)("nationality")
Json("match")("referees")(2)("id")
Json("match")("referees")(2)("name")
Json("match")("referees")(2)("nationality")
Json("match")("referees")(3)("id")
Json("match")("referees")(3)("name")
Json("match")("referees")(3)("nationality")
Json("match")("referees")(4)("id")
Json("match")("referees")(4)("name")
Json("match")("referees")(4)("nationality")
The two answers below show how to use a function that I wrote, PrintJSONAccessors(), to unravel the collections and arrays produced by JsonConverter.ParseJson().
Using VBA and VBA-JSON to access JSON data from Wordpress API
How to get, JSON values to Work in VBA-JSON

Related

Excel to json file from Excel VBA

I want to have a button in an excel document (using VBA) which, takes in the data on the sheet and outputs a file (.json). Which I can then use later on within a web page.
I my excel data looks like this:
Note I would like to have more than one instance of data.
I want it to generate a json file in the format of:
{
"Excel_test": [
{
"name" : "tesintg1",
"age" : 15
},
{
"name" : "testng2",
"age" : 1
},
{
"name" : "testing3",
"age" : 435
}
]
}
Thanks
The open-source VBA-JSON project helps with this.
Once you have installed it in your workbook's VBA project, you can do something like this:
Sub convertJson()
Dim c As Collection
Dim d As Dictionary 'Add reference to scripting runtime
Dim v As Dictionary
Dim json As String
Set c = New Collection
Set d = New Dictionary
d.Add "ExcelTest", c
For Each cell In Range("A2:A4") 'Adapt as you need
Set v = New Dictionary
v.Add "name", cell.Value
v.Add "age", cell.Offset(0, 1).Value
c.Add v
Next
json = JsonConverter.ConvertToJson(d)
Debug.Print json
End Sub
Outputs:
{"ExcelTest":[{"name":"test1","age":15},{"name":"test2","age":1},{"name":"test3","age":435}]}

How to reference and parse Json VBA Ms Access

I want to be helped to get the Arrays of Json into my table in Ms Access, how do I reference the arrays.
I have been having problems with arrays for sometime now that is why I need this sorted once and for all
With rs
.AddNew
![id] = item("id")
![riority] = item("urgent")
![Status] = item("Closed")
.Update
End With
{
"Tickets": [
{
"id": 866883,
"type": "problem",
"priority": "urgent",
"status": "closed",
"follower_ids": [
7967649388,
7967864608,
365003995267,
7897694567,
7897776387,
7899032227,
7969040908,
7898078107
]
}
]
}
I want someone to show me how to reference values from arrays in Ms access VBA
To reference arrays in VBA JSON (using dictionary), you need to specify which element in the array you want:
Using your example:
item(1)("id") will equal 866883 and item(1)("status") will equal closed. To reference the next item, use item(2)("id")

Checking if a key exists in a nested JSON element using Newtonsoft JSON and VB.NET

Using Newtonsoft Json with VB.NET I am trying to read some nested keys/elements within a block of JSON.
The JSON looks like this and is held in string strSuppliedJSON:
{
"seller": {
"id": 123,
"name": "Seller Name",
"address1": "Seller address1",
"country": "Seller country"
},
"buyer": {
"id": 987,
"name": "Buyer name",
"address1": "Buyer address1",
"country": "Buyer country"
},
"interview": {
"call_id": 123,
"vin": "The vin from the machine section",
"call_date": "2019-12-31 23:59:59",
"questions": ["Question1", "Question2", "Question3", "Question5", "Question5"],
"triggers": [{
"question": "Question1",
"answers": ["Answer1", "Answer2"]
}]
}
}
Before reading the values I need to make sure some of the keys exist using ContainsKey.
The following works fine:
Imports Newtonsoft.Json
Imports Newtonsoft.Json.Linq
' Create a dictionary
Dim dictionary As IDictionary(Of String, JToken) = JObject.Parse(strSuppliedJSON)
' Check if key exists - interview:triggers
If JObject.Parse(dictionary("interview").ToString()).ContainsKey("triggers") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
End If
However, when trying to check or read the interview:triggers:question things are falling over.
Using this, fails:
' Check if key exists - interview:triggers
If JObject.Parse(dictionary("interview").ToString()).ContainsKey("triggers") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
Else
' interview:triggers DOES exist, now check if the question exists - interview:triggers:question
If JObject.Parse(dictionary("interview")("triggers").ToString()).ContainsKey("question") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
End If
The line that throws the error is:
If JObject.Parse(dictionary("interview")("triggers").ToString()).ContainsKey("question") = False Then
And the error is:
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1
So I then tried to create a sub-dictionary of just the interview element.
I used this:
' Create a sub-dictionary of just the Interview element
Dim subDictionary As IDictionary(Of String, JToken) = JObject.Parse(dictionary("interview").ToString())
And if I then do Response.Write(subDictionary) I now see a smaller subset of my JSON, as expected:
{
"call_id": 123,
"vin": "The vin from the machine section",
"call_date": "2019-12-31 23:59:59",
"questions": ["Question1", "Question2", "Question3", "Question5", "Question5"],
"triggers": [{
"question": "Question1",
"answers": ["Answer1", "Answer2"]
}]
}
But then when trying to use my new subDictionary in the exact same way to see if the question key exists:
' Check if key exists - interview:triggers:question
If JObject.Parse(subDictionary("triggers").ToString()).ContainsKey("question") = False Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
I get the exact same error of:
Newtonsoft.Json.JsonReaderException: Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1
Even though the line of code is identical!
How do I check if the nested key interview:triggers:question exists in my JSON, and what its value is?
Look closely at "triggers" in the JSON: it's actually an array of objects, not an object. You need to index the array before you can access "question".
Also note that every time you call ToString, you're reserializing something that you just deserialized with Parse. You don't need to do that. Parse the JSON once into a JObject and then reuse that object.
My VB-fu isn't great; I wrote this in C# and then converted it but the critical part is Dim first As JToken = triggers(0). This gets the first array element, on which you can get the value associated with "question".
Dim suppliedObject As JObject = JObject.Parse(strSuppliedJSON)
Dim interview As JToken = suppliedObject("interview")
Dim triggers As JToken = If(interview IsNot Nothing, interview("triggers"), Nothing)
If triggers Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers"
Else
Dim first As JToken = triggers(0)
Dim question As JToken = If(first IsNot Nothing, first("question"), Nothing)
If question Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If
End If
It fails because your path is not proper.
[] is an Array. You write out the name for single items (within {}), for arrays you write index (number).
And why are you parsing multiple times? Use what you already have, for example:
Dim JsonResp As JObject = JObject.Parse(<JSON>)
'Now there's multiple ways to do the same, here's one
If JsonResp ("interview")("triggers")(0)("question") Is Nothing Then
strAllChecksPassed = False
result = "ERROR: JSON element not found: interview:triggers:question"
End If

Json to Excel (multi level)

I know how to parse JSON to Excel with VBA but I have a problem with multi-level JSON.
Example :
{
"Level1": [{
"String1": "Data1",
"Level 2": [{
"String2": "Data2",
"String3": "Data3",
"Level3": [{
"String4": "Data4",
"String5": "Data5"
}]
}]
}]
}
How to get everything?
The { means a dictionary so you access by key, the [ means a collection so you access by index. "" means a string literal so you read as is. Test the data type and handle as required. Below I use a JSON parser to read in your JSON string from a cell A1. After adding the .bas from that link to your project you then add a reference via VBE > Tools > References > Microsoft Scripting Runtime.
I use a sub EmptyDict which I call recursively to test whether the current object is a dictionary or collection and then loop until I empty each dictionary. For each collection I shift one column to the right.
As mentioned in the comments, you would tailor to the output format you want in the sheet.
The tree structure you are descending looks like:
VBA:
Option Explicit
Public r As Long, c As Long
Sub readValues()
Dim json As Object, item As Object
Set json = JsonConverter.ParseJson([A1])("Level1")(1) 'dictionary
r = 1: c = 1
EmptyDict json
End Sub
Public Sub EmptyDict(ByVal dict As Object)
Dim key As Variant, item As Object
Select Case TypeName(dict)
Case "Collection"
For Each item In dict
c = c + 1
r = 1
EmptyDict item
Next
Case "Dictionary"
For Each key In dict
If TypeName(dict(key)) = "Collection" Then
EmptyDict (dict(key))
Else
With ThisWorkbook.Worksheets("Sheet2")
.Cells(r, c) = dict(key)
End With
r = r + 1
End If
Next
End Select
End Sub

Dynamic json parse in vb.net

I need to parse a simple Json, but the answer is not allways the same, in the past i have created a class with tools like http://jsonutils.com/ but in this case one of the parameter changes.. this is an example of the JSON:
{"success": 1,
"message": "Registros recuperados",
"data": {
"rows": [
{
"id_jugador": "454",
"nombre": "ALEXANDER",
"apellido": "CABRERA",
"id_equipo": "5",
"equipo": "TIBURONES",
"hr": "21"
}
]
},
"total": 1
}
The problem is that the last item (hr in this case, changes some time will be hits, or have some other name), until now i have been using something like this..
jsonObjectIntance = JsonConvert.DeserializeObject(Of jsonObjectClass)(jsonString)
how do I parse it if the last parameter changes?? the hr itself con be other baseball statistics like hits, or doubles, triples, strikeouts, etc... , thanks!
thanks to #OneFineDay for the link, the difficult part for me was the access to data inside the object.
Dim item As Object = JsonConvert.DeserializeObject(Of Object)(json)
Dim success as string = item("success")
Dim data As Object = item("data")
Dim rows As JArray = data("rows")
DataGridView1.DataSource = rows