VBA-tools / VBA-JSON

JSON conversion and parsing for VBA
MIT License
1.74k stars 566 forks source link

Erroneously trying to process objects as arrays #259

Open Syakado opened 9 months ago

Syakado commented 9 months ago

Hello Guys.

Allow me to use a translation tool.

If you try to parse JSON of the following format, json_ParseKey branch and I get Err.Raise 10001. I want to solve the problem of processing an array as a key, since it is not a wrong JSON format. Can you help me?

Function test()
    Dim target As String
    target = "[ {'List': [ '11111' ] } ]"
    Dim json As Dictionary
    Set json = JsonConverter.ParseJson(target)
End Function
houghtonap commented 9 months ago

You need to reread the project readme as well as Microsoft's website on the VBA.Collection and Scripting.Dictionary objects. VBA-JSON maps a Javascript Array to a VBA.Collection and a Javascript Object to a Scripting.Dictionary.

Your sample Javascript document is a Javascript Array, but you declared the variable to hold the return value as a Scripting.Dictionary which is not the correct object that matches your Javascript document. Also, in VBA you need to use the Set command to set the value of an object. See also, Microsoft VBA Reference on their website.

Hope that helps.

Nick-vanGemeren commented 9 months ago

Andrew, are you having issues with your chatbot? ;} Javascript is not relevant here.

Syakado, the text string you provided does not give an error 10001 (with version v2.3.2). It does give an error 13 (Type mismatch). After fixing Dim json As Object the function works as expected. If you are getting error 10001 in json_ParseKey with a different text, then there is something wrong with a key:value pair; the error message text is more specific. Examples are:

The ECMA standard specifies that a key is a string enclosed in double quotes. Unquoted single-word keys and keys using single quotes {'key': value} are VBA-JSON extensions. VBA-JSON does not support non-unique keys.

You cannot use a JSON Object or a JSON Array as a key, unless you encode it as a quoted string.

========== If this solves your problem, please close the issue here.