0

I have a simple JSON array like this:

["123", "124", "321"]

The expected result should be 3. How can I count the number of elements without using regular expressions and always working with a JSON object?

I tried using the following code:

Dim jsonStr, jsonArray, elementCount
jsonStr = "[""123"", ""124"", ""321""]"
Set jsonScriptControl = CreateObject("ScriptControl")
jsonScriptControl.Language = "JScript"
Set jsonArray = jsonScriptControl.Eval("(" + jsonStr + ")")
elementCount = jsonArray.Length
MsgBox "Number of elements: " & elementCount

However, I encountered an error message:

Microsoft VBScript runtime error: An ActiveX component cannot create an object: 'ScriptControl'.
5

1 Answer 1

0

ScriptControl is a 32 bit object. Therefore, your script must be run using a 32 bit scripting host:

c:\windows\syswow64\wscript.exe My32bitScript.vbs

Alternatively, for 64bit, use htmlfile:

jsonStr = "[""123"", ""124"", ""321""]"
Set html = wscript.CreateObject("htmlfile")
Set window = html.parentWindow
window.execScript "var json = " & jsonStr, "JScript"
Set jsonArray = window.json 
elementCount = jsonArray.Length
MsgBox "Number of elements: " & elementCount
Sign up to request clarification or add additional context in comments.

Comments

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.