Splitting API GET Data

Hey all I am successfully getting data via URLConnection in my web app hosted on Xojo Cloud. However, I am trying to find documentation on how to split the results among multiple fields or columns in a list box. If I receive data back that looks something like this:

{"success":true,"data":{"id":7236,"username":"BOB","avatar":"/user/avatar/7236_1648829847.png","groups":[]}

How do I pull out each part of that? Thanks!

That information is JSON:

{
  "success": true,
  "data": {
    "id": 7236,
    "username": "BOB",
    "avatar": "/user/avatar/7236_1648829847.png",
    "groups": []
  }
}

This may help:
https://documentation.xojo.com/topics/file_management/reading_and_writing_data_in_json_format.html#reading-and-writing-data-in-json-format

1 Like

This is really helpful and I have been able to write most of the code for it but I can’t seem to figure out the syntax to get to id
I have tried many iterations of the below. Help?

uID.Value(["data"]["id"])
uID.ValueAt(2)

Unclear if you used ParseJSON or JSONItem, but assuming the former:

var data as Dictionary = uID.Value( "data" )
var id as integer = data.Value( "id" )
1 Like

Alright, so here is what I am working with. I am receiving the data from the POST and I can write that to a text area and see it. Then nothing from the Parse on is working. Am I missing something?

Var authInfo As New JSONItem 
authInfo.Value("email") = "emailaddress@gmail.com"
authInfo.Value("password") = "DaPassw0rd"
authInfo.Value("device") = "web"

dim url As String = "https://api.APIWEBSITE.COM/api/v1/auth/login?categoryId=1"

dim socket1 As New URLConnection
socket1.SetRequestContent(authInfo.ToString, "application/json")
Var data As String = socket1.SendSync("POST", url, 300)

Var jData As JSONItem = ParseJSON(data)
Var dict As Dictionary = jData.Value("data")
app.uID = dict.Value("id")

ParseJSON and JSONItem are mutually exclusive. ParseJSON returns either a Dictionary or a Variant array depending on the JSON.

Try this:

Var jData As Dictionary = ParseJSON(data)
Var dict As Dictionary = jData.Value("data")
app.uID = dict.Value("id")
2 Likes

Few problems here.

  • 300 secs is too much for a login connection retention
  • You are not protecting against broken JSON responses.
// Just an example, read it, don't copy/paste

Var jsonResponse As String = socket1.SendSync("POST", url, 20)

Var json As JSONItem
Try
  json = New JSONItem(jsonResponse)
Catch // Something wrong
  json = Nil
End

If json = Nil Then Return "Error" // Some strategy you need to think

Var jsonData As JSONItem = json.Value("data")
Var uId As Integer = jsonData.Value("id")

1 Like

Thanks I incorporated your suggestions!