If you haven’t already, make sure to listen to the previous episode 153 about XML. Many of the concepts that I described there apply to JSON as well.
Why should you consider using JSON?
Well, you can use JSON, or XML, or any of several other techniques anytime you need to store and retrieve, or send and receive information. First of all, XML can do everything that JSON can do and then some. And JSON comes really close to being able to match XML. There are a few things that XML can do that JSON can’t such as embedding comments inside the XML, or validating that the XML adheres to a valid format. But even then, don’t be surprised if the standards change and evolve so that JSON fully matches XML.
To me, it really just comes down to a choice between which formatting you like best. Usually though, JSON will be shorter just because you don’t have to worry about ending each XML tag with a duplicate ending tag.
So what is JSON?
I’m not going to try describing the syntax. That would involve telling you where to put commas, colons, and quotation marks. I’ll stick to the concepts. And there are two main concepts.
The first is name value pairs. This is a simple concept and really just means that a value has an associated name. If we’re building an adventure game and the hero has a health of 100 and a speed of 5, then both of those values have a name. The value doesn’t have to be so simple. It could be a string, a number, true or false, have no value at all, an array, or represent an object that has it’s own name value pairs. Each of these named values is in no particular order.
The second concept has to do with arrays that I just mentioned as one of the value types. An array is actually ordered, so if an item comes before another item, then that means something. At least it means something as far as JSON is concerned. Think of array values just like the first concept of named values only without the name. With an array, it’s the order that identifies values.
Make sure to listen to the full episode for an example of how you might use JSON to store information about characters.