getting error while parsing json file in excel vba

Nihar1991

New Member
Joined
Jan 8, 2019
Messages
2
Can anyone please help me on this. i have tried many methods but failed every time.

My json file looks like:

{"hMapCB":{

"AU":{
"listAdmins":[
{"UserName":"SelAuto1DN1","Password":"SelAuto1DN1"},
{"UserName":"SelAuto1DN1","Password":"SelAuto1DN1"},
{"UserName":"SelAuto1DN1","Password":"SelAuto1DN1"},
{"UserName":"SelAuto1DN1","Password":"SelAuto1DN1"},
{"UserName":"SelAuto1DN1","Password":"SelAuto1DN1"}]},

"CA":{
"listAdmins":[

{"UserName":"selauto1castg2@im.com","Password":"P@ssw0rd123"},
{"UserName":"selauto1castg2@im.com","Password":"P@ssw0rd123"},
{"UserName":"selauto1castg2@im.com","Password":"P@ssw0rd123"},
{"UserName":"selauto1castg2@im.com","Password":"P@ssw0rd123"},
{"UserName":"selauto1castg2@im.com","Password":"P@ssw0rd123"}]}
}}


And i need to read user name and password of type listAdmins and save it into the excel sheet where country name matches like (AU or CA).


Sample Code:


Sub ihdt()


Dim FSO As New FileSystemObject
Dim JsonTS As TextStream
Dim JsonText As String
Dim Parsed As Dictionary

' Read .json file
Set JsonTS = FSO.OpenTextFile("C:\Users\inarin01\Desktop\Macro learning\CredentialStage1_old.json", ForReading)
JsonText = JsonTS.ReadAll
JsonTS.Close

Set Parsed = JsonConverter.ParseJson(JsonText)

Set events = Parsed("hMapCB")

For Each k In events
MsgBox k
If k = "AU" Then
Set listAdmins = events(k)("listAdmins")
For Each v In listAdmins
MsgBox v
'Debug.Print , "participant", v
Next v
End If

Next

End Sub

 

Excel Facts

Format cells as time
Select range and press Ctrl+Shift+2 to format cells as time. (Shift 2 is the @ sign).
Almost:

You want:
Rich (BB code):
For Each k In events
    MsgBox k
    If k = "AU" Then
        Set listAdmins = events(k)("listAdmins")
        For Each v In listAdmins
            Debug.Print v("UserName"), v("Password")
        Next v
    End If
End Sub

Though this would be more efficient:
Rich (BB code):
For Each k In Array("AU", "CA")
    Set listAdmins = events(k)("listAdmins")
    For Each v In listAdmins
        Debug.Print v("UserName"), v("Password")
    Next v
Next k
 
Upvote 0

Forum statistics

Threads
1,215,465
Messages
6,124,977
Members
449,200
Latest member
Jamil ahmed

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