0

I've imported the VBA JSON library into my VBA project but I can't get it to create the JSON object properly. I've fallen at the first hurdle.

Even the example code that they give isn't working:

Sub test()


Dim Json As Object
Set Json = JsonConverter.ParseJson("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")

' Json("a") -> 123
' Json("b")(2) -> 2
' Json("c")("d") -> 456
MsgBox Json("c")("e") = 789

End Sub

This gives the following answers:

a

2

456

null

I've selected the Microsoft Scripting Runtime library and added the VBA Dictionary class.

3
  • If you don't need to support Mac you can remove the dictionary class Commented Feb 23, 2018 at 4:56
  • MsgBox Json("c")("e") = 789 here you're passing the result of adding a new key and value to Json("c") to Msgbox: that operation doesn't return a value, so there's nothing to show. I can't explain that first "a" though Commented Feb 23, 2018 at 4:59
  • Looking at the locals window, it's created a dictionary of the first tier of nodes but nothing below that. Commented Feb 23, 2018 at 5:40

1 Answer 1

1

I'm getting the expected results.

Sub TestJson()

    Dim Json As Object
    Set Json = JsonConverter.ParseJson("{""a"":123,""b"":[1,2,3,4],""c"":{""d"":456}}")

    Debug.Print Json("a")        '--> 123
    Debug.Print Json("b")(2)     '--> 2
    Debug.Print Json("c")("d")   '--> 456
    Json("c")("e") = 789         'create new key and value under "c"
    Debug.Print Json("c")("e")   '--> 789

End Sub

Be very careful using the Watch window when dealing with Dictionary objects: just having an active watch on a dictionary key can cause that key to get added.

Sign up to request clarification or add additional context in comments.

2 Comments

IDNKT - I did not know that re: Watch Window and keys.... why on earth would it do that?
You're exactly right. I got so stressed about the watch window not showing the object properly, it didn't even occur to me that it could be working. Thanks very much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.