Navigating JSON data & pulling values - Help!

Hey all,

So I am pulling in JSON data and I am able to create a JSONItem from it OK. Here would be some JSON:

{ "expand":"schema,names", "startAt":0, "maxResults":1000, "total":3, "issues":[ { "expand":"operations,editmeta,changelog,transitions,renderedFields", "id":"11111", "self":"https://jira.thewebsite.com/rest/api/latest/issue/11111", "key":"PROJ-001", "fields":{ "summary":"This would be another summary 1" } }, { "expand":"operations,editmeta,changelog,transitions,renderedFields", "id":"22222", "self":"https://jira.thewebsite.com/rest/api/latest/issue/22222", "key":"PROJ-002", "fields":{ "summary":"This would be another summary 2" } }, { "expand":"operations,editmeta,changelog,transitions,renderedFields", "id":"33333", "self":"https://jira.thewebsite.com/rest/api/latest/issue/33333", "key":"PROJ-003", "fields":{ "summary":"This would be another summary 3" } } ] }

I figured out to access the “top level” values, such as:

dim intTotalSubTasks as integer intTotalSubTasks = jsonSubTasks.Value("total")

…but I can’t figure out how I can access the “issues” array, or further yet the “summary” listed in “fields” … I have tried various for loops that try to reference the values, but I am banging my head against a wall at this point!

Can anyone throw me a bone? I am having a hard time understanding how to “go down a level” to a sub-value (aka, down to “fields” inside “issues”, and then into “summary”)

I understand I will need a loop to put the data into an array/structure/class/dictionary/whatever, but I can’t get anything to work. I have looked at some search hits on the forum for JSON parsing, but I have more levels than those examples.

Thanks to anyone who can help!!

[code]Dim total As Integer = json.Value(“total”)
Dim issues As JSONItem = json.Value(“issues”)

For i As Integer = 0 To issues.Count - 1
Dim issue As JSONItem = issues(i)
Dim expand As String = issue.Value(“expand”)
Dim fields As JSONItem = issue.Value(“fields”)
Dim summary As String = fields.Value(“summary”)
Next[/code]

Maybe it’s interesting to have a look at how to work with JSON using the new framework.
Apart from it being so much faster, I personally find it much easier.

edit: https://www.youtube.com/watch?v=CrjYyy-6B_s

Thanks Eli! That makes me understand it better; each “layer” is treated as its own JSONItem!

This is my code that works:

[code] Dim issues As JSONItem = jsonSubTasks.Value(“issues”)
For i As Integer = 0 To issues.Count - 1
Dim issue As JSONItem = issues(i)
Dim id As String = issue.Value(“id”)
Dim selfy As String = issue.Value(“self”)
Dim key As String = issue.Value(“key”)
Dim fields As JSONItem = issue.Value(“fields”)
Dim summary As String = fields.Value(“summary”)

  Dim newrows() As String = Array(key,id,summary,selfy)
  lstSubtasks.AddRow(newrows)
  
Next[/code]

Marco, I don’t have much experience with Dictionary objects… I can wrap my head around the JSONItem idea; how would Eli’s code look if it used the new Xojo framework JSON Parse command?

Thanks all; Xojo forum is always a life saver!! I have learned so much from you guys.

This is how you would do it using the new Xojo Framework:

[code]
Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(kJSON)
Dim total As Integer = jsonDict.Value(“total”)
Dim issues() As Auto = jsonDict.Value(“issues”)

For Each issue As Xojo.Core.Dictionary In issues
Dim expand As Text = issue.Value(“expand”)
Dim fields As Xojo.Core.Dictionary = issue.Value(“fields”)
Dim summary As Text = fields.Value(“summary”)
Next[/code]

Thanks Paul!

I can now understand how the JSON Parsing works thanks to you!

I changed my code over to use the new Xojo Framework :

[code]

Dim jsonDict As Xojo.Core.Dictionary = Xojo.Data.ParseJSON(strTheRealResult.ToText)
Dim intTotalSubTasks As Integer = jsonDict.Value("total") 
Dim issues() As Auto = jsonDict.Value("issues") 

For Each issue As Xojo.Core.Dictionary In issues
  Dim id As Text = issue.Value("id") 
  Dim selfy As Text = issue.Value("self")
  Dim key As Text = issue.Value("key")
  
  Dim fields As Xojo.Core.Dictionary = issue.Value("fields") 
  Dim summary As Text = fields.Value("summary")
  
  Dim newrows() As String = Array(CStr(key),CStr(id),CStr(summary),CStr(selfy))
  lstSubtasks.AddRow(newrows)
  
  
Next[/code]

Works great! I just had to some String/Text conversions but no big deal! I am just used to using Strings :slight_smile:

You guys are great. My project is shaping up thanks to your help!!