Friday the 13th: Return of JSON
Today we see the web in a new light. Many web developers are commutatively choosing their standards of choice - HTML5, CSS3, JavaScript, and JSON. Of these standards, JSON is the only one to truly be questioned by developers and has taken its time in climbing the ladder. However I do believe today JSON has grasped the integrity of the Web Dev community and we are seeing extensive growth, but to understand its struggle to stardom we need to look at what JSON really is.
As defined by json.org: JSON - (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
In other words JSON is simply a coding notation based on JavaScript that you can use to hold and organize your data. <- That'll do for our basic definition. Now, based on this definition alone the fight broke out (like 6+ years ago) over why we should use JSON while XML (Extensible Markup Language) is in town. So when do we draw the line from XML to JSON? I mean why fix something that isn't broken, right?
Unlike XML, JSON was built with programmers in mind. XML holds great when transforming a document into an exchangeable format. It gives semantic meaning to text within documents, while JSON suits better for structuring data. Dion Almaer of ajaxian.com put it this way:
JSON is a better fit for Web services that power Web mashups and AJAX widgets due to the fact that it is essentially serialized Javascript objects which makes it fit better client side scripting which is primarily done in Javascript. That’s it. XML will never fit the bill as well for these scenarios without changes to the existing browser ecosystem which I doubt are forthcoming anytime soon. - Dion Almaer
So we see that JSON hasn't taken over XML, but one should realize it is the focal point of web development. So when you're up all night coding that web app you put your heart and soul into, I encourage you dive into using JSON for handling your data structures.
I'll leave you with some code snippets and important facts below. Thanks for reading.
If you were to build a web app that fetched and processed data using a cart system. This would be one method of using JSON in your code.
Delegate the data here:
<json:object> <json:property name="itemCount" value="${cart.itemCount}"/> <json:property name="subtotal" value="${cart.subtotal}"/> <json:array name="items" var="item" items="${cart.lineItems}"> <json:object> <json:property name="title" value="${item.title}"/> <json:property name="desc" value="${item.desc}"/> <json:property name="imageUrl" value="${item.imageUrl"/> <json:property name="price" value="${item.price}"/> <json:property name="qty" value="${item.qty}"/> </json:object> </json:array> </json:object>
This will create the following JSON code:
{ itemCount: 2, subtotal: "$15.50", items:[ { title: "Awesome Book", description: "Bestselling book of facts by I. M. Fake", imageUrl: "/images/books/12345.gif", price: "$10.00", qty: 1 }, { title: "Javascript Pocket Reference", description: "Easy to bring along JS reference", imageUrl: "/images/books/56789.gif", price: "$5.50", qty: 1 } ] }
Here are some good factoids about JSON!
- JSON does not have a <[CDATA[]]> feature, so it is not well suited to act as a carrier of sounds or images or other large binary payloads. JSON is optimized for data. Besides, delivering executable programs in a data-interchange system could introduce dangerous security problems.
- JSON has six types of values: objects, arrays, strings, numbers, booleans, and null.
- JSON is an unordered container of name or value pairs.
- Whitescpace can be added or taken away without change to the data structure. This helps in making the code more human readable, while keeping it machine readable. Taking away Whitespace will result in small files for easier transfer of your data.
- Despite its relationship to JavaScript, it is language-independent, with parsers available for most programming languages.
- The official Internet media type for JSON is application/json.
- JSON is easily embedded into HTML with <script> tags.
- The JSON filename extension is .json.
- The JSON format is often used for serializing and transmitting structured data over a network connection.
- JSON is primarily used to transmit data between a server and web application, serving as an alternative to XML.
May 13, 2011 in Computer Science, JavaScript
Tagged as: JavaScript, JSON
