Json and loop through each line

jaryszek

Board Regular
Joined
Jul 1, 2016
Messages
213
Hello,

i have json files (a few) like here:

Code:
{
  "Test" : "VB_variable", "Test2" : true,
  "Test3": "http://google.com",
  "attributes":[
       {   "cell":"VB_answer", "property": "highAvailability" }
  ] 
}

All what i want to get list all values starting from VB*.

I am trying to build macro based on code from link below:

https://github.com/VBA-tools/VBA-JSON



Expected result here:
VB_variable
VB_answer

How can i achieve this using loop or some other code?

Best,
Jacek
 

Excel Facts

Excel Can Read to You
Customize Quick Access Toolbar. From All Commands, add Speak Cells or Speak Cells on Enter to QAT. Select cells. Press Speak Cells.
Try this (with JsonConverter from the link).

Code:
Public Sub Test()
    Dim JSONstring As String, ParsedJSON As Object
    JSONstring = "{ ""Test"" : ""VB_variable"", ""Test2"" : true, ""Test3"": ""http://google.com"", ""attributes"":[ { ""cell"":""VB_answer"", ""property"": ""highAvailability"" }  ]}"
    Set ParsedJSON = JsonConverter.ParseJson(JSONstring)
    With ThisWorkbook.Worksheets(1)
        .Cells.Clear
        JSONfindValues ParsedJSON, .Range("A1"), "VB_"
    End With
End Sub


Private Function JSONfindValues(JSONvar As Variant, destCell As Range, findValue As String) As Long

    Dim n As Long
    Dim key As Variant
    Dim i As Long
    
    n = 0
    
    If TypeName(JSONvar) = "Dictionary" Then
        For Each key In JSONvar.keys
            n = n + JSONfindValues(JSONvar.item(key), destCell.Offset(n, 0), findValue)
        Next
    ElseIf TypeName(JSONvar) = "Collection" Then
        For i = 1 To JSONvar.Count
            n = n + JSONfindValues(JSONvar(i), destCell.Offset(n, 0), findValue)
        Next
    Else
        If InStr(1, JSONvar, findValue, vbTextCompare) > 0 Then
            destCell.Offset(n, 0).Value = JSONvar
            n = n + 1
        End If
    End If

    JSONfindValues = n
    
End Function
 
Upvote 0
wow excellent !

thank you!
working!

Another solution:

Code:
Set JsonTS = FSO.OpenTextFile(objFile, ForReading)


JsonText = JsonTS.ReadAll


For i = 1 To InStrRev(JsonText, "Vn_")
    tempvar = InStr(i, JsonText, "Vn_")
    result1 = InStr(tempvar, JsonText, """")
    
    longer = result1 - tempvar
    
    resultAll = Mid(JsonText, tempvar, longer)
    
    i = result1 + 1
    
    counter = counter + 1
    
    Sheets(1).Cells(counter, 1).Value = resultAll
    
Next i


JsonTS.Close

Best,
Jacek
 
Upvote 0

Forum statistics

Threads
1,214,950
Messages
6,122,438
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top