Json to Excel (multi level) - json

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

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 adding nested dictionaries & collection in VBA/Json Ms Access 2016

Our company require to send invoices details for assessment to our Tax consultant in Json format, this means every invoice generated must pass through the Tax consultant for authentication. If an invoice is not correctly generated the Tax consultant send it back as rejected. The problem here we are not able to get the correct Json format after exporting Ms access 2016 invoices data
I have a code below which is almost complete for exporting Ms Access data into the correct Json format, but I’m the following two things below:
Current Json format:
{
"PosSerialNumber":"",
"IssueTime":"",
"TransactionType":0,
"PaymentMode":0,
"SaleType":0,
"Items":{
"ItemId":1,
"Description":"Apple"
"Barcode":"458630036",
"Quantity":8,
"UnitPrice":2,
"Discount":0,
"Taxable":[
"A",
"T"
]
}
}
The following two things below:
(1) As you can see its missing a square bracket on "Items":{ , the correct one is supposed to be "Items":[{ as well as the closing ]
(2) I want to add also (“Total”,120, “IstaxInclusive”: true) so that the final code should look like below:
Below is the current MS Access VBA code we are using to try and achieve the required goal
VBA code used to generate the above Json format:
Option Compare Database
Option Explicit
Private Sub Command0_Click()
Dim foo As New Dictionary
Set foo = New Dictionary
Dim Noor As Dictionary
Set Noor = New Dictionary
Dim hoo As New Collection
Dim goo As New Dictionary
Set goo = New Dictionary
Dim Zoo As New Dictionary
Set Zoo = New Dictionary
Dim Koo As New Collection
Dim json As String
With foo
.Add "PosSerialNumber", Me.txtchris
.Add "IssueTime", Me.txtAddress
.Add "TransactionTyp", 0
.Add "PaymentMode", 0
.Add "SaleType", 0
.Add "Items", Noor
Noor.Add "ItemID", 1
Noor.Add "Description", "Apple"
Noor.Add "BarCode", "4589630036"
Noor.Add "Quantity", 8
Noor.Add "UnitPrice", 2
Noor.Add "Discount", 0
Noor.Add "Taxable", hoo
hoo.Add "A"
hoo.Add "T"
End With
Dim member As Variant
For Each member In foo
Next
MsgBox JsonConverter.ConvertToJson(foo, Whitespace:=3), vbOKOnly, "Audited by C H"
End Sub
Required format:
{
"PosSerialNumber":"",
"IssueTime":"",
"TransactionType":0,
"PaymentMode":0,
"SaleType":0,
"Items":[{
"ItemId":1,
"Description":"Apple"
"Barcode":"458630036",
"Quantity":8,
"UnitPrice":2,
"Discount":0,
"Taxable":[
"A",
"T"
]
"Total":120,
"IsTaxInclusive":true,
"SP":0
}
]
}
Square brackets in JSON indicate an Array. JsonConverter will wrap collection or Arrays in [].
You can either wrap Noor in an Array() or add it to a collection
Array
.Add "Items", Array(Noor)
Collection
Dim NoorCollection As New Collection
NoorCollection.Add Noor
.Add "Items", NoorCollection

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

how to extract elements in json string in vba

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

How to pick JSON nodes given in an Excel table for each field?

Hi I am quite new to VBA and JSON. I want to parse a JSON script using a VBA macro.
I already have an Excel table with each field and it's corresponding JSON path.
While trying to pick the value from Excel table, it is read as string and, quotes are added in the beginning and end of the string. The quotes " in the beginning and the end of the path of the JSON variable makes it impossible to read the value from the JSON script.
For example, if the path is project->name, the location in Excel table is ("project")("name"). But after reading it in VBA it becomes "("project")("name")". With the extra quotes " in the path, the location is not getting identified in the VBA code.
{
"quiz": {
"sport": {
"name": "Basketball",
"Questions":{
"question1": "Which one is correct team name in NBA?",
"question2":"Who is your favorite player",}
}
}
}
For this JSON script, I have created an Excel table with paths of question1 and question2:
("quiz")("sport")("name")("question1")
("quiz")("sport")("name")("question2")
The following code runs a loop and identify path of question1 first and returns "Which one is correct team name in NBA?" and do the same for question2.
But, item(path) is returning an empty string while writing the path completely in the code returns the correct value.
Set jsonObject = JsonConverter.ParseJson(JsonScript) 'Parse Json from GitHub
For Each item In jsonObject("data")
For i = 1 To nrow ' loops through rows with path for each field
Path = ws2.Range("C" & i).Value 'path of each field
MsgBox item(Path) 'returns Empty
question1 = item("quiz")("sport")("name")("question1") 'returns question1 value:Which one is correct team name in NBA?
Next
Next
You can't apply in that way particularly with the (). You can have the paths, as a comma separated list of path elements (no spaces), in the sheet and do the following.
You also need to remove the additional "," after player.
Note: I am testing the UBound of the array generated by the split on the "," of the string read in from the sheet and held in the array (arr) at the current index i. This ensures I apply the write nested syntax to retrieve the required value.
I am not sure where you are getting "data" from. I can't see it in the string provided. You would amend according to your actual JSON if different.
Data:
Output:
VBA:
Option Explicit
Public Sub GetInfoFromSheet()
Dim json As Object, jsonSource As String, paths(), i As Long, ws As Worksheet, arr() As String
Set ws = ThisWorkbook.Worksheets("Sheet1")
jsonSource = ws.[C1]
Set json = JsonConverter.ParseJson(jsonSource)
paths = Application.Transpose(ws.Range("A1:A3").Value)
For i = LBound(paths) To UBound(paths)
arr = Split(paths(i), ",")
Select Case UBound(arr)
Case 2
Debug.Print json(arr(0))(arr(1))(arr(2))
Case 3
Debug.Print json(arr(0))(arr(1))(arr(2))(arr(3))
End Select
Next i
End Sub
JSON string in C1:
{ "quiz": { "sport": { "name": "Basketball", "Questions":{ "question1": "Which one is correct team name in NBA?", "question2":"Who is your favorite player"} } } }