I'm trying to write Excel VBA code that calls an API to fetch data, massage the data, then output the cleaned data over a range of cells in the spreadsheet.
I run into an error while trying to parse the API response with JsonConverter.
I followed the instructions at https://github.com/VBA-tools/VBA-JSON to install VBA-JSON, and imported JsonConverter.bas into my script, and added the reference to "Microsoft Scripting Runtime".
I installed Tablacus as a 64-bit ScriptControl.
Here's the beginning of the code, and where it hits the error:
Function IsValidJson(jsonString As String) As Boolean
Dim regex As Object
Set regex = CreateObject("VBScript.RegExp")
regex.Pattern = "^[\s\S]*\{[\s\S]*\}[\s\S]*$"
IsValidJson = regex.Test(jsonString)
End Function
Sub RefreshData()
Dim url As String
Dim http As Object
Dim json As Object
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim trading_pairs() As String
Dim trading_pairs_eth() As String
Dim trading_pairs_usd() As String
Dim final_pairs() As String
Dim final_prices() As Double
Dim final_volumes() As Double
Dim pair As String
Dim volume As Double
Dim price As Double
Dim row As Integer
Dim JsonConverter As Object
Dim dict As New Dictionary
Set http = CreateObject("MSXML2.XMLHTTP")
Set JsonConverter = CreateObject("ScriptControl")
JsonConverter.Language = "JScript"
JsonConverter.AddCode "function parseJson(jsonString) { return JSON.parse(jsonString); }"
' Fetch all trading pairs with BTC as the quote currency
url = "https://api.pro.coinbase.com/products/BTC-USD/book"
http.Open "GET", url, False
http.send
If IsValidJson(http.responseText) Then
Set json = JsonConverter.ParseJson(http.responseText) ' <- error happens here
ReDim trading_pairs(json("bids").Count - 1)
For i = 0 To json("bids").Count - 1
trading_pairs(i) = json("bids")(i)("price")
Next i
Else
MsgBox "Invalid JSON string"
Exit Sub
End If
This is part of the whole script, it's quite long.
The line Set json = JsonConverter.ParseJson(http.responseText) is where it is throwing error 438.
I have a IsValidJson function validating the "http.responseText", before attempting to parse it with JsonConverter.
It passes the validation check, but still throws error 438 when trying to parse.
Code originally generated by ChatGPT.
The JSON-Converter you linked isn't a class that needs to be instantiated. The code that you display looks like a mixture of (at least) two different things.
The code you need to use is rather simple.
The call
JsonConverter.ParseJson(http.responseText)is the call to the functionParseJsonof the moduleJsonConverter, not an object. You can omit the module name and simply writeSet json = ParseJson(http.responseText)- that's exactly the same.Therefore you need to delete the definition of the variable
JsonConverterelse the compiler will assume that you want to call a methodParseJSonof that object and therefore you will get the 438 error.I have no clue why you or ChatGPT want to create a
ScriptControlobject, remove that line and the assingments toLanguageandAddCodealso.I am also not sure if the function
IsValidJsonis doing it's job, I have removed the call to it. The ParseJSon function will raise an error (10001) if the JSON is not valid.Once this works, you need to walk thru the parsed JSon, and your code will raise the next errors soon. Just some hints:
I had a quick look to
http.responseText, for me it looked like this:After the call to
ParseJSon, you have a dictionary as result in variablejson(check the content with the debugger). The elementbidsis an array with 1 element, and this element itself is an array with 3 elements (3 numbers). The JSonConverter converts an array to aCollection. Collections in VBA are 1-based, not 0-based. To get an idea: