About Me

I'm just someone struggling against my own inertia to be creative. My current favorite book is "Oh the places you'll go" by Dr. Seuss

Tuesday, September 18, 2007

JSON, XML and the Relational Model part 2

In math, there is something called set theory.

In set theory, there is something called a set.

A set is simply a list of items with two rules. One of the rules is that order is not significant:

{1,3,2,4} is the same set as {3,4,2,1}


The other rule is that repeats are not significant:

{1,1,2,3,4,3} is the same set as {1,2,3,4}


A set can contain more than just numbers. It can contain anything, such as words:

{Julie, Bob, George, Ringo}


and even other sets:

{{5,3,1},{7,9,2},{3,2,1}}


In set theory, there's another concept called a tuple. A tuple is basically the same as a set, except that the two rules above do not apply. For a tuple, order does matter, and you can have repeats.

These two concepts: sets, and tuples, are the basic building blocks for the three popular data formats listed in the title of this series (And many more). There are plenty of other useful concepts in math for data structures. However, computer languages seem to stick mostly to these two for structured data.

Since JSON is essentially a javascript object, it follows javascript's rules. Here's an example of JSON:

{ 
example:"Object",
name:"bob",
array:["George","Ringo","Paul"]
}


As you can see, this is basically a set, with the members
example:"Object"
,
name:"bob"
and
array:["George","Ringo","Paul"]


each of these members is a double: a tuple with two members. For instance, the first member is the tuple
{example, "Object"}
. The array tuple,
(array:["George","Ringo","Paul"])
is also a tuple with two members.
{array, ["George","Ringo","Paul"]}
. The second member of this tuple, is also a tuple.
{array, {"George", "Ringo", "Paul"}}


So we can see that JSON is composed rather simply of nested sets, tuples, and values. Tuples can contain other tuples, tuples can contain sets, sets can contain tuples, and sets can contain sets.

No comments: