Home » excel » excel – VBA-JSON: Add entry to Existing File

excel – VBA-JSON: Add entry to Existing File

Posted by: admin May 14, 2020 Leave a comment

Questions:

I’m using excel to modify existing JSON with the VBA-JSON library and I’m effectively trying to do what .push from javascript does, adding another entry to the nested array.

There’s this previously posted question which illustrates what I’m trying to do but in the scope of creating a JSON file.

VBA-JSON Create nested objects

The accepted answer while helpful, doesn’t show me how to add to what I already have without rebuilding it entirely. I can picture a mess of loops to parse my existing file into strings, then do something similar to what is in the post. I’m curious if there’s another way to do it.

'download json

Set json = JsonConverter.ParseJson(H.responseText)

Debug.print json(1)("entries")(1)("Date")
'2019-09-25

json(1)("entries")(2)("Date") = "2019-09-26"
'error 9 Subscript out of Range

'upload json

[
    {
        "Entries": [
            {
                "Date": 2019-09-25,
                "a": 1,
                "b": 2
            }
        ]
    }
]

I’m able to access and modify existing entries but not add to it. I’m guessing this is because the library parses it as a defined array rather than variable? Any ideas on a simple solution or will I have to deconstruct my file into dictionaries/collections and rebuild it first?

How to&Answers:

VBA Json is really just a cheap (and effective) converter/deconverter of a json string into a nested set of dictionaries and collections. So you don’t have to “deconstruct [your] file into dictionaries/collections and rebuild it” since… it already is. This is why it’s necessary to add in the Microsoft Scripting Runtime library to use it.

The struggle you are dealing with is that you are treating every object in your json object as a dictionary. Instead you want to add(pop) to the entries collection. A good way to keep these separate is that in VBA JSON (as far as I’ve seen) if an object is a key/value pair, then it’s a Dictionary and if it’s just an object without a key then it’s a Collection. I’m certain there is more nuance there, but it works well in this scenario.

Consider:

Sub test()
    Dim Json As Object
    Set Json = JsonConverter.ParseJson("[{""Entries"":[{""Date"": 2019-09-25,""a"": 1,""b"": 2}]}]")

    Dim entry1 As Object
    Set entry1 = Json(1)("Entries")

    Stop '<--inspect Locals and see that we have a Collection here

    'Add to the collection (different then adding to a dictionary)
    'We will add a dictionary to the collection though
    Json(1)("Entries").Add New Dictionary

    'Now add to the dictionary.
    Json(1)("Entries")(2).Add "Date", "2019-09-26"

    Stop

    Debug.Print JsonConverter.ConvertToJson(Json)

    'Alternatively you could create the dictionary and add your keys, then `pop` your entries
    Dim Entry As Dictionary
    Set Entry = New Dictionary
    Entry.Add "Date", "2019-09-26"
    Entry.Add "a", "2"
    Entry.Add "b", "3"

    Json(1)("Entries").Add Entry

    Debug.Print JsonConverter.ConvertToJson(Json)
End Sub

This should spit out what you are after (I think):

[{"Entries":[{"Date":"2019-09-25","a":1,"b":2},{"Date":"2019-09-26"}]}]

I guess another way to think through this is that if we want to add a new array to our Entries then we have to add a new Dictionary object to the Entries collection. Then we can add into the dictionary our key/value pairs like Date, a, and b.