0

i have a json object that has a json array. I need to iterate array and print the values. I am using excel [vba].I am very new to VBA. Requesting anyone to help me out.

Set sr= CreateObject("MSScriptControl.ScriptControl") sr.Language = "JScript" Set Retval = MyScript.Eval("(" + newString + ")") MsgBox Retval.Earth.Fruits(0).name 

when i execute the above piece i am getting 'Object doesn't support this property or method'.

I need to iterate all the names under Fruit

1
  • my newString will hold : newString will hold : { "Earth": { "Fruits": [ { "name":"Mango" }, { "name":"Apple" }, ..... ] }, }
    – Danesh
    CommentedAug 24, 2019 at 20:18

1 Answer 1

1

I would use a json parser e.g. jsonconverter.bas as can use with 64bit and 32bit and doesn't represent the same security risk as scriptControl.

Jsonconverter.bas: Download raw code from here and add to standard module called jsonConverter . You then need to go VBE > Tools > References > Add reference to Microsoft Scripting Runtime.

Your json object is a dictionary with an inner dictionary Earth containing a collection Fruits (where Fruits is the key). The items in the collection are dictionaries with keys of "name" and values are the fruits. The [] denotes collection and {} dictionary.

Option Explicit Public Sub test() Dim s As String, json As Object, item As Object s = "{""Earth"":{""Fruits"":[{""name"":""Mango""},{""name"":""Apple""},{""name"":""Banana""}]}}" Set json = JsonConverter.ParseJson(s) For Each item In json("Earth")("Fruits") Debug.Print item("name") Next End Sub 

Example with regex:

Public Sub test() Dim s As String s = "{""Earth"":{""Fruits"":[{""name"":""Mango""},{""name"":""Apple""},{""name"":""Banana""}]}}" PrintMatches s End Sub Public Sub PrintMatches(ByVal s As String) Dim i As Long, matches As Object, re As Object Set re = CreateObject("VBScript.RegExp") With re .Global = True .MultiLine = True .IgnoreCase = False .Pattern = """name"":""(.*?)""" If .test(s) Then Set matches = .Execute(s) For i = 0 To matches.Count - 1 Debug.Print matches(i).SubMatches(0) Next i Else Debug.Print "No matches" End If End With End Sub 
7
  • Hi, thank you for the Swift reply.... As per my organization policy I am not allowed to use unapproved items. I came across same solution but I am not supposed to use that... Any other way.
    – Danesh
    CommentedAug 24, 2019 at 21:02
  • Are you aware of the risks of using scriptControl?! That aside... yes you could use regex or split.
    – QHarr
    CommentedAug 24, 2019 at 21:03
  • regex example added
    – QHarr
    CommentedAug 24, 2019 at 21:08
  • 2
    github.com/VBA-tools/VBA-JSON is just a code module you import into your project - no additional dll etc. If your company policy would prefer you to use a scriptcontrol over VBA-JSON then that's crazy. ScriptControl has potentially huge security issues.CommentedAug 25, 2019 at 0:50
  • @TimWilliams: Out of curiosity, what kind of security issues are known with ScriptControl? Do you have some links, maybe? Thanks in advance.
    – AHeyne
    CommentedAug 26, 2019 at 5:31

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.