Select Page

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.

JSON stands for JavaScript Object Notation and comes from the JavaScript language. But even so, it’s a good choice for representing data in any language.

If you’re using JavaScript, there is a method you can call to read JSON. This method is called eval which is short for evaluate. However, I don’t recommend that you call this method because it can run code just as easy as read data. You’re better off using a library that has the ability to parse JSON data as just data. This will be the approach you’ll take with any other programming language anyway.

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. Or you can also read the full transcript below.

Transcript

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.

JSON stands for JavaScript Object Notation and comes from the JavaScript language. But even so, it’s a good choice for representing data in any language.

If you’re using JavaScript, there is a method you can call to read JSON. This method is called eval which is short for evaluate. However, I don’t recommend that you call this method because it can run code just as easy as read data. You’re better off using a library that has the ability to parse JSON data as just data. This will be the approach you’ll take with any other programming language anyway.

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.

If you have a bunch of values and they don’t really need individual names, and especially if they’re all the same or similar, then an array is a good choice. But if you need to identify specific values by name, then the name value is best. Let me give you an example.

Lets’ say that we need to store the current information for several game characters. Some of these characters could have a name and we could organize the data around their names. But in this case, I’d probably use an array. The order doesn’t really matter but it could in other situations. That’s okay. Even though the array is ordered, all that really means is that JSON will keep the same order. It doesn’t mean that the game characters have to have some kind of order to them.

So I’d use an array of objects. Where each object holds name value pairs with the information for that character. One of the name value pairs could be called name and would hold the actual character name. The health and speed could be other named values along with many other named values describing things such as where the character is in the game, special abilities, and things that the character is carrying.

And this brings up another aspect of JSON that I’d like to highlight. So far, we have an array of characters. Each character is an object with its own named values. And one of those named values is an inventory of items. The best way to represent the collection of items is with another array.

That’s the point I wanted to make clear. JSON can be made up of whole hierarchies of data that switch between named values and arrays whenever needed.