JavaScript Objects and JSON can be a bit confusing, so let's take a minute to discuss what JSON is, as well as some of the advantages of the format for handling data. JSON stands for JavaScript Object Notation, and it's a text format that makes it easy to share data between devices like clients and servers. JSON owes its origin and it's based on the way JavaScript objects work. So in effect, it's related to and close to, but not exactly like, JavaScript objects. Although it's originated with JavaScript, it's available in many different languages, like C, Ruby, Perl, Python, and PHP.
Because it is smaller and easier to convert into a data structure, it's a great alternative to other formats like XML. One of the advantages to using JSON is how easy it is to read. JSON uses minimal formatting--really just a few special characters in addition to the data. So here I've created a JSON object with a few key-value pairs. The keys are on the left-hand side and the value of the keys are on the right hand side. So for example, the key would be the name, and the value will be my name, Ray Villalobos, and the position and courses, et cetera, et cetera.
JavaScript can understand this as an object that you can then access. Another advantage to JSON is that it's super easy to parse. So for example, you can create an object with this data and then pass it into a JavaScript object by simply using a JavaScript command called JSON.parse. You can then do things like access any of the items within the object just by typing in something like info.name or info.position or info.courses and then specify an item in the array.
So you could say item number 1, and it would output the second item, because arrays are 0 index, so this would be building Facebook applications. If you take a look at the same data expressed in XML notation, which is another very popular format, you can see that it is a lot larger and more verbose than JSON. This means that JSON data will take less space and load faster into your web applications. Plus, parsing an XML object can be complicated and time consuming, whereas JSON is easily mapped into a JavaScript object and so it takes less time to process.
In recent years, JSON has gotten so popular that companies like Twitter have even chosen to discontinue support for XML output off the streams. As you can see on this page, version 1.1 of the Twitter API does not support XML output. For these and other reasons, JSON is the best format to use when working with data in JavaScript. If you want to work with JSON in other formats, it has many implementations in popular languages. If you go to this URL, and scroll down to the bottom of the page, you can see all the languages that JSON supports.
Released
3/15/2013- What is JSON?
- Creating simple data
- Debugging JavaScript objects in the browser
- Communicating across sites with JSONP
- Rotating with jQuery Cycle
Share this video
Embed this video
Video: What is JSON?