Xojo JSON file

Hello,
is there any easy way to load, update and store JSON files/data?

JSONItem class is built specifically for this.

Var person As New JSONItem
// This object is manipulated like a dictionary
person.Value("Name") = "John Doe"
person.Value("Age") = 32
person.Value("Married") = True
person.Value("Spouse") = "Jane Doe"

Var kids As New JSONItem
// This object is manipulated like an array
kids.Add("John Jr")
kids.Add("Jamie")
kids.Add("Jack")
kids.Add("Josie")
kids.AddAt(0,"Jonah")
kids.RemoveAt(2)
person.Value("Kids") = kids

Person.Compact = True
Var s As String = person.ToString

TextArea1.Text = s
Var js As String = "{""Name"":""John Doe"",""Age"":32,""Kids"":[""Jonah"",""John Jr""],""Married"":true,""Spouse"":""Jane Doe""}"
Var j As New JSONItem(js)
1 Like

As for storage, you can write a file directly from String using TextOutputStream, and load it into a String using TextInputStream.

Along with JSONItem, you can use ParseJSON to convert the String directly to a Dictionary or Array(), and GenerateJSON to convert it back to String.

2 Likes

Thanks. It’s working now. Another question.
Is it possible to send directly a JSON file over websocket or just the JSON data in the file?

Hi Sebastian - I believe there are other ways to pass the JSON data, but I’m only familiar with one, which is via URLConnection, and “POST” which puts text-data in the body of the message, vs. GET which puts it in the URL.

URLConnection.SetRequestContent(myJSONItem.ToString, "application/json")
URLConnection.Send("POST", server_address + "/myHandleURL", 20)