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

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

Posted by: admin May 14, 2020 Leave a comment


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",
                "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?  
How to&Answers:

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.


enter image description here


enter image description here


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"} } } }