| Article Index |
|---|
| 1. JSON |
| 2. Basic Data Types |
| 3. JSON by Example |
| 4. JSON and Data Exchange |
| 5. Perspective |
Transport and exchange of data is a common problem in the IT industry. There are a number of standards available that can be integrated into custom applications. JSON is one of them. JSON is an acronym for JavaScript Object Notation. It is a lightweight text-based open standard intended for data exchange.
This article illustrates how to utilize JSON to transport data between programs written in arbitrary programming languages.
1. JSON
JSON is text-based. So it is human readable and easy to store or transmit over computer networks. JSON has its origin in the programming language JavaScript, but it is not bound to any JavaScript related libraries or components. JSON is often used for serializing objects to transmit them via networks. Due to the fact that JSON is simply storing key-value pairs and lists of values, it may be handled as an alternative to XML, because you are able to map hierachical data structures with custom encodings and dataypes.
If you do know how to write applications in JavaFX or how to embed active elements from AJAX (Web 2.0) frameworks into web pages you will get an idea how to work with JSON and how to define custom data types.
2. Basic Data Types
JSON supports the following basic types:
- Number (integer od real)
- String (double-quoted Unicode Strings with backsplash escaping)
- Bollean (true / false)
- Array (ordered sequence of values, separated by commas, enclosed in square brackets)
- Object (a collection key-value pairs, separated by commas, enclosed in curly braces)
- null
By including custom type detinitions in a JSON data stream you are able to map additional data types to the JSON format. But the available types should suffice to map even binary data encoded in hexadecimal or other formats utilizing the ASCII key set.
3. JSON by Example
Now we will take a look to a JSON encoded datastream.
{
"firstName": "John",
"lastName": "Doe",
"age": 61,
"address": {
"streetAddress": "25 Eisenhauer Street",
"city": "New York",
"state": "NY",
"postalCode": "10021"
},
"phoneNumber": [
{ "type": "home", "number": "212 555-1234" },
{ "type": "fax", "number": "646 555-4567" }
]
}
The subsequent example shows a XML document containing the same data as in the example before.
<Person firstName="John"
lastName="Doe"
age="61">
<address streetAddress="25 Eisenhauer Street"
city="New York"
state="NY"
postalCode="10021"/>
<phoneNumber type="home">212 555-1234</phoneNumber>
<phoneNumber type="fax">646 555-4567</phoneNumber>
</Person>
4. JSON and Data Exchange
For virtually any programming language there are libraries available to read and write JSON data streams. Many of the popular scripting languages do contain native support for JSON data streams. Due to the fact that JSON is an extremly simple data format and it is an open standard is is very likely that you will not get any problems when exchanging data streams between several platforms and applications written in different programming languages.
Python for example contains support for JSON in its core libraries. The following example illustrates how to convert a Python dictionary into a JSON data stream. When generating a JSON data stream without any additional parameters the output will not contain any whitespaces or newline characters.
>>> import json
>>> print json.dumps({'My age': 55, 'My size (cm)': 178}, sort_keys=True, indent=4)
{
"My age": 55,
"My size (cm)": 178
}
The website http://www.json.org/ contains a comprehensive overview of programming languages and libraries supporting JSON. Additionally you will find details how to encode / decode JSON data streams if you are planning to implement your own nifty library.
5. Perspective
Just give it a try! If you are playing arond with JSON and you are looking for new fields of application you even could find ways to implement RPC style remote procedure calls. By following this approach and implementing simple wrappers you should be able to utilize a JSON based communication protocol instead of utilizing heavy-weight technologies like CORBA. This example should give you just an idea what is possible by following the KISS principle (Keep It Simple & Stupid) and by utilizing JSON as medium for data exchange.
JSON is an extremely simple-to-use standard. There is a broad support for this format, JSON libraries are freely available and the propsed way to excahnge data is reliable, stable and proven. (The description of the JSON standard is available since 2002)
Starting with JSON a number of additional markup languages did arise. YAML is just one exemplar for this species.
Additional resources:




