How to read a json string

I need to read this string
{
“patient”: {
“id”: “PZ001”,
“surname”: “Rossi”,
“name”: “Mario”,
“sex”: “M”,
“date_of_birth”: “1985-04-04”
},
“anthropometry”: {
“height_cm”: 178,
“weight_kg”: 82,
“wrist_circumference_cm”: 17,
“waist_circumference_cm”: 95,
“hip_circumference_cm”: 102,
“neck_circumference_cm”: 40
}
But I can’t with the code below.
I can read the first part if I change it to an array like this.
[
{
“id”: “PZ001”,
“surname”: “Rossi”,
“name”: “Mario”,
“sex”: “M”,
“date_birth”: “1985-04-04”
}
]
For the code, I created a class called patient with
the properties surname, date_of_birth, id, name, and sex
and an anthropometry class with the related properties,
two sub
json2Patients(j as JSONItem) as patient()
dim result() as patient
For c as Integer = 0 to j.Count-1
dim jChild as JSONItem = j.Child(c)
dim p as patient = patientJ2O(jChild)
if p<>nil then result.Append(p)
Next
Return result

patientJ2O(j as JSONItem) as patient
dim p as new patient
p.id = j.Lookup(“id”,“”)
p.Surname = j.Lookup(“surname”,“”)
p.Name = j.Lookup(“name”,“”)
p.BirthDate = j.Lookup(“birthdate”,“”)
p.sex = j.Lookup(“sex”,“”)

'dim jAnth as JSONItem = j.Lookup(“anthropometry”, new JSONItem)
'dim anth as new anthropometry
'anth.height = jAnth.Lookup(“height_cm”, 0)
'anth.weight = jAnth.Lookup(“weight_kg”, 0)
'anth.wrist = jAnth.Lookup(“wrist_cm”, 0)
'anth.waist = jAnth.Lookup(“waist_cm”, 0)
'anth.hips = jAnth.Lookup(“hip_cm”, 0)
'anth.neck = jAnth.Lookup(“neck_cm”, 0)
This part is not enabled, it gives me an error
Return p

for reading this code
dim restUrl as String = “https://xxxxxxxxx.php
dim response As String
dim username as String = userrest
dim password as String = passwrest
dim socket as new HTTPSocket
dim timeout as Integer = 30
'socket.SetRequestHeader(“Authorization”, “Basic " + EncodeBase64(username+”:"+password))
'response = socket.get(RestUrl, timeout)
I don’t use the downloaded one for testing
response =textrest.text
response = DefineEncoding(response, Encodings.UTF8)

dim cl as Integer
dim j As New JSONItem(response)
j.Compact = False

patients() = json2Patients(j)

if response.len =0 then exit
for w=0 to patients.Ubound
msgbox patients(w).id
msgbox patients(w).surname
msgbox patients(w).name
msgbox patients(w).sex
msgbox patients(w).birthdate
next
what do I need to modify to read the first string. The code shown isn’t entirely my own work
but rather your previous help.

Hi @Mario_Graziani ! It might be easier to help if we started with the basics. Are you able to paste some conforming JSON into this post - we can see what you’re starting with? Maybe output it to the console and cut-paste it directly:

System.DebugLog(YOUR_JSONItem.ToString)

The posted JSON is missing the final }

{
	"patient": {
		"id": "PZ001",
		"surname": "Rossi",
		"name": "Mario",
		"sex": "M",
		"date_of_birth": "1985-04-04"
	},
	"anthropometry": {
		"height_cm": 178,
		"weight_kg": 82,
		"wrist_circumference_cm": 17,
		"waist_circumference_cm": 95,
		"hip_circumference_cm": 102,
		"neck_circumference_cm": 40
	}
} // <---that one
1 Like

With @Andrew_Lambert ‘s repaired JSON (good eye!) you should be able to access the values you need like this:

Var patient_record As New JSONItem(TextArea1.Text)

Var patient As JSONItem = patient_record.Value(“patient”)

Var anthropometry As JSONItem = patient_record.Value(“anthropometry”)

System.DebugLog(patient.Lookup(“surname”, “NOT FOUND”))

System.DebugLog(anthropometry.Lookup(“height_cm”, “NOT FOUND”).StringValue)

13:58:00 : My Application Launched
13:58:01 : Rossi
         : 178
13:58:05 : My Application Ended

I tried but it gives this error

Var patient_record As New JSONItem(textrest.Text) : Ok
Var patient As JSONItem = patient_record.Value(“patient”) : this item does not exist
Var anthropometry As JSONItem = patient_record.Value(“anthropometry”) : this item does not exist

Hi Mario - maybe throw a breakpoint just after you create patient_record (see below), and then examine the variables - see if when you drill down into ‘patient_record’ if you see the JSON content you expect. Maybe even cut-n-paste the JSON from that into this post so we can check it out.

Var patient_record As New JSONItem(textrest.Text) : Ok
Break
Var patient As JSONItem = patient_record.Value(“patient”) : this item does not exist

Hi William, I tried, and the result is this:
Compact True
DecimalFormat -0.0###########
EscapeSlaces True
IndentSpacing 2
ToString
{“patient”:{“id”:“PZ001”,“surname”:“Rossi”,“name”:“Mario”,“sex”:“M”,“date_of_birth”:“1985-04-04”},“anthropometry”:{“height_cm”:178,“weight_kg”:82,“wrist_circumference_cm”:17,“waist_circumference_cm”:95,“hip_circumference_cm”:102,“neck_circumference_cm”:40}}

It seems right to me. If you want and can, you can download the project file at this link. It would be a huge favor.
https://www.realdieta.it/public/testrest.zip

These are compile errors right? If the json itself was at fault, you would be getting KeyNotFoundExceptions.

Since the two things you are extracting are known to be JSONitems, instead of Value, try using Child.

…and make sure you’re not using curly quotes in Xojo!

Ciao Mario,

What about using an SQLite data base instead ?

From a Record, you can export data as JSON (and Import ad JSON if needed)…

And you can keep your UI, just add some code…

This (using a data base) allows you to make statistics in eventual reports…
How many Men/Women, birth by years (how many), height, waist circum…, weight and so on… (age also

That’s a lot of work just to decode a json string

2 Likes

Yes, but I added some other advantages of a DB use as a suggestion; and I may have forget some.

Also, OP l,now what he want to do better than me and that suggestion can help him (he may realize later he needs a DB).

Now, to stay in the question, you are correct.

Emile, I need to insert that data into a database,
but I have no problem doing that. My problem is reading that type of string, divided by topic (patient, anthropometry, and other topics), the posted string is a small part of the actual string. If I remove the topic from the string:
{
“id”: “PZ001”,
“surname”: “Rossi”,
“name”: “Mario”,
“sex”: “M”,
“date_of_birth”: “1985-04-04”
}
Then with this little code I can read it.
response = TextArea1.text.trim
response = DefineEncoding(response, Encodings.UTF8)
Var patient As New JSONItem(response)
msgbox patient.Value(“surname”)
msgbox patient.Value(“name”)
msgbox patient.Value(“id”)