I’m working on parsing JSON data from a streaming platform’s chat API but I’m stuck on how to properly loop through the nested structure. The JSON contains user information organized in different categories and I need to access all the usernames.
Dim apiUrl = "https://api.example.tv/users/" & chatRoom.Name.Replace("#", "") & "/participants"
Dim responseData As String = String.Empty
Dim client As New WebClient()
responseData = client.DownloadString(apiUrl)
Dim jsonData As JToken = JToken.Parse(responseData)
For Each element As JToken In jsonData("participants")
' Need help here - how do I get all the users from the different groups?
Next
The main issue is that I can’t figure out how to extract all the participant names from the different categories in the JSON response. Any suggestions on the right approach?
Based on your code structure, you’re missing the nested iteration that streaming APIs typically require. The participants object usually contains multiple arrays for different user types. What worked for me was first checking the JSON structure by debugging the response - print out jsonData("participants").ToString()
to see exactly what categories exist. Then you need something like For Each category As JProperty In jsonData("participants")
followed by another loop For Each user As JToken In category.Value
to access individual user objects. Most streaming platforms return usernames in a field called “name”, “username”, or “display_name” within each user object. Don’t forget to handle cases where some categories might be empty arrays, which happens frequently with moderator or VIP lists. I’d also recommend adding error handling around the API call since streaming APIs can be unreliable during peak hours.
I ran into something similar when working with Twitch’s API last year. The trick is understanding that streaming APIs often nest users under different role categories like moderators, subscribers, regular viewers etc. You’ll need to iterate through each category first, then access the users within each one. Try modifying your loop to check what properties exist in the participants object first. Use jsonData("participants").Children()
to see all the category keys, then loop through each category’s user array. Most streaming platforms structure it as something like participants.moderators[]
, participants.subscribers[]
etc. Each user object should then have a username or display_name property you can extract. The key is that double loop - outer for categories, inner for actual users within each category.