How to read JSON

I am getting below jason string from my web service.

{“http_code”:200,“response_code”:“SUCCESS”,“response_msg”:“Your file has been uploaded.”,“data”:{“upload_id”:33181645,“file_name”:“E89093BB-0613-41EC-82D4-F1F57872A18F.jpg”,“date_added”:1621406337,“date_delete”:1631918337,“user_id”:146747,"_url":“https:\mydomain.com”}}

i tried to load this string in JSONItemm but then i am not able to read values of jason.
Its not showing any child element on loading this string.

Does any one know how can i read above jason string?

It’S JSON and there is a JSONItem class for Xojo.

You would do

dim j as new JSONItem(text)
dim http_code as integer = j.value("http_code")

Please, show the actual code you use to read the string returned by your web service.

I think you should parse the string to JSONItem first.
https://documentation.xojo.com/api/text/json/parsejson.html

Hello Christian,
I am able to read Http_code, response_code etc. which are outside of “data” element.
But Facing problem to read values in “data” element.
e.g i want to read “_url”

Have-you done that:

Var JsItem As New JSONItem

JsItem = "Place here the value returned from the Web Service"

value returned from the Web Service:
{“http_code”:200,“response_code”:“SUCCESS”,“response_msg”:“Your file has been uploaded.”,“data”:{“upload_id”:33181645,“file_name”:“E89093BB-0613-41EC-82D4-F1F57872A18F.jpg”,“date_added”:1621406337,“date_delete”:1631918337,“user_id”:146747,"_url":“https:[mydomain.com](http://mydomain.com)”}}

and then ?

To help you with the “Then” part, check the Examples projects.

i am using following code-

Var teams As New JSONItem
teams.Load(textData) //textData has jason reply of web service

For t As Integer = 0 To teams.Count - 1
MsgBox teams.Value(“data”) //i want to read all elements here

Next

You need to create a JSONItem from the response, then walk the JSONItem to the data you need.

var response as new JSONItem( jsonResponse ) ' jsonResponse contains the JSON string data that you received
if response.HasKey( "data" ) then ' Make sure that the response contains the "data" node
  var data as JSONItem = response.Value( "data" ) ' Assign the data node to its own item for easy use
  var uploadId as Integer = data.Value( "upload_id" ) ' Get a value of data
  
  '// Do something with it.
end if

Hello Anthony
This worked for me, Thank you.

Happy to help!

Note: I’ve fixed the title and moved this topic to General from Target-> Windows.

One important concept is that a JSON Object can contain Arrays [ ] and Objects { }, and a mix of them nesting each other. And Arrays have lists of items indexed by an integer index, zero based, and Objects contains items as named properties. Arrays and Objects can be nested creating simple lists; or complex deep structures.

Simple example of JSON strings starting as Array or Object:

Var js1 As String = "[ ""str0"", ""str1"", {""prop_double"" : 123.34 , ""prop_string"" : ""str3"" } ]"
Var js2 As String = "{ ""prop0"": ""str0"", ""prop1"" : [ 123.34 , ""str-prop1"" ] }"

Var json As New JSONItem(js1)

Var s, s1 As String = json.Value(1) // zero based = "str1"
Var d As Double = JSONItem(json.Value(2).ObjectValue).Value("prop_double") // 123.34

break // Inspect the values here

json = New JSONItem(js2)

s = json.Value("prop0") // "str0"
s1 = JSONItem(json.Value("prop1").ObjectValue).Value(1) // "str-prop1"

break // Inspect the values here