# JSON oh my life

**URL:** <https://forum.xojo.com/t/json-oh-my-life/77192>\
**Category:** General\
**Created:** [September 8, 2023, 8:25am UTC](https://forum.xojo.com/t/json-oh-my-life/77192 "2023-09-08T08:25:14Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Emma\_Frost](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/a5b964/32.png) [@Emma\_Frost](https://forum.xojo.com/u/Emma_Frost)\
**Post date:** [September 8, 2023, 8:25am UTC](https://forum.xojo.com/t/json-oh-my-life/77192/1 "2023-09-08T08:25:14Z")

</div>

Never written code to parse JSON before. Been tackling it in Xojo all weekend.

Good God, who could have imagined such an easily human readable format would be such a damn nightmare to traverse in code!? Hahaha, I mean, I get it. I understand why the JSONItem is structured the way that it is, and now that I’m starting to wrap my mind around it it, it’s quite an elegant mechanism, all things considered.

But oh lord, this has been a journey. 2/10, don’t recommend. LOL

---

<div class="post-metadata">

**Author:** ![Thomas\_Kaltschmidt](https://forum.xojo.com/letter_avatar_proxy/v4/letter/t/f0a364/32.png) [@Thomas\_Kaltschmidt](https://forum.xojo.com/u/Thomas_Kaltschmidt)\
**Post date:** [September 8, 2023, 8:44am UTC](https://forum.xojo.com/t/json-oh-my-life/77192/2 "2023-09-08T08:44:49Z")

</div>

Hi Emma, i agree and think Xojo should include more and better code examples in the documentation and example projects.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [September 8, 2023, 10:59am UTC](https://forum.xojo.com/t/json-oh-my-life/77192/3 "2023-09-08T10:59:30Z")

</div>

I much prefer XML, although many years ago I decried it on the basis that it was a hugely wasteful file format.  
Eventually I realised it has a lot of advantages over a binary storage - human almost-readable and editable, plus any app that reads such a file can decide what parts it wants to use.  
You can stick new bits in without breaking existing apps.

But apparently that’s old hat, and JSON is where it’s at these days.  
Yet to see something to change my mind, but it may happen. 🙂

---

<div class="post-metadata">

**Author:** ![Thom\_McGrath](https://forum.xojo.com/user_avatar/forum.xojo.com/thom_mcgrath/32/192_2.png) [@Thom\_McGrath](https://forum.xojo.com/u/Thom_McGrath)\
**Post date:** [September 8, 2023, 12:25pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/4 "2023-09-08T12:25:30Z")

</div>

Yeah I remember the XML days. Its robustness is also its weakness. It takes far more code to deal with XML than JSON, both reading and writing. You can’t just turn a dictionary into XML. But JSON’s popularity has a lot to do with JavaScript, since every damn thing is coded with JavaScript these days. Being able to transfer/store objects with a single command is wonderful. So it makes a lot of sense to use for APIs. Other languages have to come along for the ride.

Even then, I’m not sure I’d really like to return to the XML days. I wrote the XML-RPC client classes for REALbasic/Xojo and boy am I glad that isn’t necessary these days. The fact of the matter is XML is a total chore to work with compared to JSON. I’d rather give up some features than deal with the endless boilerplate required to handle XML.

---

<div class="post-metadata">

**Author:** ![MarkusR](https://forum.xojo.com/user_avatar/forum.xojo.com/markusr/32/685_2.png) [@MarkusR](https://forum.xojo.com/u/MarkusR)\
**Post date:** [September 8, 2023, 12:34pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/5 "2023-09-08T12:34:03Z")

</div>

> [@Emma\_Frost](#):
>
> JSON

i write my xojo kanban board data to JSON and read it back. a time-consuming development and not intuitive.  
Xojo still have a lack of object serialization included.  
in C# you just have to inherit a class and you can save the object to XML and create the object from XML.  
i serialized a entire invoice with many objects and list of objects without great effort.  
same for web services or web api controller.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [September 8, 2023, 12:43pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/6 "2023-09-08T12:43:35Z")

</div>

> [@Thom\_McGrath](#):
>
> I’d rather give up some features than deal with the endless boilerplate required to handle XML.

Interesting.  
JSON always looks like a real nightmare to me.  
I suppose ‘once you get used to it’ 🙂  
I quite like XQL

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [September 8, 2023, 12:57pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/7 "2023-09-08T12:57:10Z")

</div>

> [@Emma\_Frost](#):
>
> who could have imagined such an easily human readable format would be such a damn nightmare to traverse in code!?

Plan you data structure, then just read/write as you’ve planned. Usually this is very simple.  
If you are making something not very simple, not obvious, document it.

Then traversing it is piece of cake.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [September 8, 2023, 2:25pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/8 "2023-09-08T14:25:06Z")

</div>

> [@Jeff\_Tullin](#):
>
> Eventually I realised it has a lot of advantages over a binary storage - human almost-readable and editable, plus any app that reads such a file can decide what parts it wants to use.  
> You can stick new bits in without breaking existing apps.

Apart from the human readable bit (albeit a little bit of practice and a hex editor), you can construct binary formats in an extensible way so that new data can be added or injected without breaking existing apps. IFF was created EONs ago with that capability. AIFF, TIFF etc etc… [Interchange File Format - Wikipedia](https://en.wikipedia.org/wiki/Interchange_File_Format)

What I love the most about binary formats is speed…

When I set about creating my own help viewer for App Wrapper (and it is in Sleep Aid) because Apple broke Apple help for third party apps (Monterey I think), I wanted to use a database, but because Xojo refuses to support read-only databases, I had to build my own. I don’t want to load up the entire index to be able to find a page (as I would with XML or JSON), so I researched hash tables. When a customer searches for something it uses the hash table to narrow down the results, then loads those into memory, before narrowing down even further. It’s quick enough that you’d never know it’s loading on demand, nor do you have to wait when you open the help viewer for the data to be loaded, or copied someone else on the disk to work around something that Xojo simply don’t want to improve.

---

<div class="post-metadata">

**Author:** ![Jeff\_Tullin](https://forum.xojo.com/user_avatar/forum.xojo.com/jeff_tullin/32/693_2.png) [@Jeff\_Tullin](https://forum.xojo.com/u/Jeff_Tullin)\
**Post date:** [September 8, 2023, 2:45pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/9 "2023-09-08T14:45:50Z")

</div>

Yup. IFF. And If I recall properly the OLEDocument that Office used…

---

<div class="post-metadata">

**Author:** ![Jeffrey\_Massung](https://forum.xojo.com/user_avatar/forum.xojo.com/jeffrey_massung/32/11607_2.png) [@Jeffrey\_Massung](https://forum.xojo.com/u/Jeffrey_Massung)\
**Post date:** [September 8, 2023, 2:53pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/10 "2023-09-08T14:53:31Z")

</div>

One thing I do in my project that uses `JSONItem` is to make a couple classes that represent the various “objects” in the JSON and then I implement `Operator_Convert` methods for them that allow me to seamlessly go back and forth:

```auto
sub Operator_Convert(json as JSONItem)
    self.GivenName = json.Value("given_name")
    self.Surname = json.Lookup("surname", "")
end

function Operator_Convert() as JSONItem
    var json as new JSONItem

    json.Value("given_name") = self.GivenName
    json.Value("surname") = self.Surname

    return json
end

```

This is nice because they build on each other. For example:

```auto
sub Operator_Convert(json as JSONItem)
    self.Mom = json.Child("mom")
    self.Dad = json.Child("dad")
end

function Operator_Convert() as JSONItem
    var json as new JSONItem

    json.Child("mom") = self.Mom
    json.Child("dad") = self.Dad

    return json
end

```

And arrays are easier:

```auto
var people() as Person

for i as integer = 0 to json.LastRowIndex
    people.Add json.ChildAt(i)
next

```

Anyway, I hope this helps a little bit. It’s made my life a whole lot easier, if it is a lot of boilerplate that I wish Xojo would do for us. 😉

---

<div class="post-metadata">

**Author:** ![James\_Dooley2](https://forum.xojo.com/user_avatar/forum.xojo.com/james_dooley2/32/14441_2.png) [@James\_Dooley2](https://forum.xojo.com/u/James_Dooley2)\
**Post date:** [September 8, 2023, 3:18pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/11 "2023-09-08T15:18:43Z")

</div>

I have been using these two [Einhugur](https://www.einhugur.com/) libs to handle XML and JSON for some time now and I find they make life much easier.

[https://www.einhugur.com/Html/JSONIIISerializationFramework/index.html](https://www.einhugur.com/Html/JSONIIISerializationFramework/index.html)

[https://www.einhugur.com/Html/XmlSerializationFramework/index.html](https://www.einhugur.com/Html/XmlSerializationFramework/index.html)

---

<div class="post-metadata">

**Author:** ![Art\_Gorski](https://forum.xojo.com/user_avatar/forum.xojo.com/art_gorski/32/704_2.png) [@Art\_Gorski](https://forum.xojo.com/u/Art_Gorski)\
**Post date:** [September 8, 2023, 4:34pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/12 "2023-09-08T16:34:30Z")

</div>

> [@Emma\_Frost](#):
>
> Good God, who could have imagined such an easily human readable format would be such a damn nightmare to traverse in code!?

I feel your pain. Parsing the first time usually gets me spectacular crashes. Variants and Dictionaries, Oh My! Using the debugger to see each step and what the data expects next was my savior.

---

<div class="post-metadata">

**Author:** ![Tim\_Parnell](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_parnell/32/161_2.png) [@Tim\_Parnell](https://forum.xojo.com/u/Tim_Parnell)\
**Post date:** [September 8, 2023, 5:54pm UTC](https://forum.xojo.com/t/json-oh-my-life/77192/13 "2023-09-08T17:54:44Z")

</div>

> [@Art\_Gorski](#):
>
> Variants and Dictionaries, Oh My!

Are a problem with the Dictionary method of parsing JSON. [I constantly recommend that you do not use that method](https://forum.xojo.com/t/probleme-with-parsejson/77093/6). The `JSONItem` class makes parsing JSON much more straightforward, and the code is self documenting.

---

<div class="post-metadata">

**Author:** ![Sam\_Rowlands](https://forum.xojo.com/user_avatar/forum.xojo.com/sam_rowlands/32/265_2.png) [@Sam\_Rowlands](https://forum.xojo.com/u/Sam_Rowlands)\
**Post date:** [September 9, 2023, 2:48am UTC](https://forum.xojo.com/t/json-oh-my-life/77192/14 "2023-09-09T02:48:36Z")

</div>

Oh, I forgot to mention that on the Mac, Apple provides a way to serialize dictionaries into XML files or binary files.

It is a lot of work to make it work with Xojo, but something that I’ve probably added a feature request for somewhere.
