JSON -> JavaScript Object Notation

In my previous post I already scratched a bit the surface of the JSON interchange format. In this post, as you might already guessed we will take a close look at this interchange format.

This simple file format is defined under the name of the final draft:  ECMA-404. This link will lead you to an PDF where everything you need to know is explained. It’s about 14 pages long. Quite the small standard and easy to understand.

Now lets start and recap what i have said in my last post about objects:

JSON Object

Any Object is composed of an arbitrary number of Key/Value pairs. Each pair is divided by an ‘:’  where left of it you will find the Key and right to it the Value associated with that Key. To be able to distinguish different pairs each pair is separated by an comma.   Those pairs can be named as items, or properties, depending of the context and mostly preference of the user. The latter one  will be mostly found when used in JavaScript because the objects “member variables” are called properties. What i have not mentioned last time is that the key is an string and all Items within an object are unsorted.

The Value associated to an Key  can then hold any type defined by the standard.

Primitive Types:

  • numbers
  • booleans
  • strings
  • undefined

Complex Types:

  • object
  • array
  • function

JSON Value

 

 

 

 

 

You might notice the discrepancy between the one I listed and the one on the picture which was taken from the standard.  We are missing two types:

  • undefined
  • function

This difference can be explained easily: the type undefined means in JavaScript that the type hold by the Variable is not yet determined the consequence is we have to cast our undefined Variable to something we can store and that would be the type null specified by the standard.

function are a bit more complicated. A Function is an set of instruction pending execution but function is an JavaScript depending type, they are bound by the environment on which the JavaScript instance is running. In other words they hold to many possible dependencies. We simply cannot guarantee that we don’t have any  dependencies and because of this missing guarantee we cannot store them in JSON.

There is one way around this limitation. But it only works with an JavaScript environment. It is possible to store a function as a string. why? because JavaScript is an interpreted language. Our source code gets not compiled like in C/C++ instead its executed line by line. We can simply evaluate the stored function at a later point by simply storing part of the source code, the function, as string in an JSON object.

But why is it possible only in JavaScript environments? Because JSON is defined as language independent file format. C/C++ or Java have no way of storing functions outside the compiled unit except for Shared Object files which follow their own specification and are not human readable whereas JSON is human readable.

JSON string

Because we spoke about strings so much lets see what an string is in JSON: a String in JSON is simple any sequence of characters between two ‘ ” ‘ and some escape sequences . Characters are in UNICODE and the escape sequences are the ones you know from C/C++ and friends. Quite straight forward i find.

 

Another type we are missing in this explanation so far is the number type.  Numbers in JSON are stored as decimal numbers and they follow the same rules as you might know already.

JSON number

Numbers can be signed or not. can be written in scientific Notation. And can represent any type of number you wish to represent, except complex numbers because they are made up of two numbers. means storing those requires double the space in the file!

JSON array

Last but not least lets take a look at the array type. An array can hold an arbitrary number of values addressable via an integer index. An sequence is encapsulated by a pair of square brackets and each item in the array is seperated by an comma.  And that is it.

Before i end this post let me show you an example where everything is used:

 

Leave a Reply

Your email address will not be published. Required fields are marked *