Why is JSON so popular? Developers want out of the syntax business.

There is a reason why JSON is becoming very popular as a data exchange format (more important than it being less verbose than XML): programmers are sick of writing parsers! But “wait”, you say – “surely there are XML parsers available for you to use so that you don’t have to roll your own…”. Yes, there are. But while XML parsers handle the low-level syntactic parsing of XML tags, attributes, etc…, you still need to walk the DOM tree or, worse, build one yourself with nothing but a SAX parser (Objective-C iPhone SDK I’m looking at you!). And that code you write will of course depend on whether the XML you need to make sense of looks like this:

<person first-name="John" last-name="Smith"/>

or this:

<person>
   <first-name>John</first-name>
   <last-name>Smith</last-name>
</person>

or this:

<object type="Person">
   <property name="first-name">John</property>
   <property name="last-name">Smith</property>
</object>

or any of the myriad of other ways one can conceive of expressing the same concept (and there are many). The standard XML parser does not help you in this regard. You still need to do some work with the parse tree.

Working with JSON is a different, and superior, experience. Firstly, the simpler syntax helps you avoid the need to decide between many different ways of representing your data (as we saw above with XML) – much less rope to hang yourself with. Usually there is only one straightforward way to represent something:

{ "first-name" : "John",
 "last-name" : "Smith" }

Even more important, if you are working in Javascript (which is very often the case when working with JSON), all you need to do is call eval on a JSON string to obtain a first-class Javascript object. This is huge. The subtle point here is that the output of an XML parser is a parse tree, not an object native to the programming language being used. With XML you are still dealing with syntax to a large degree. When you work with JSON you can go straight from a string representation to object (and back).

What makes this possible is that Javascript has syntactic constructs for describing composite data types literally. While virtually all languages have syntax for the literal description of objects of primitive types (integers (e.g. 5), strings (e.g. “hello world”)), not all languages have syntax for the literal description of objects of composite types. For instance, if you want to create a map in Java you need to do it procedurally:

Map m = new HashMap();
m.put("a", 1);
m.put("b", 2);
m.put("c", 3);
.
.
.

Java does not have literal syntax for maps. But languages such as Python and Javascript (and others) do. In Javascript we can define our map literally:

{ "a" : 1, "b" : 2, "c" : 3, ... }

As it turns out, such sub-languages are a great match for data interchange formats that are both human and machine readable.

As for XML… it’s just not the best for structured data interchange (even with some of the cool Object/XML mapping technologies out there). It works well for markup (i.e. HTML) of text but not as a language for structured data. For that developers want something they don’t have to parse all the time.

Subscribe

Subscribe to our e-mail newsletter to receive updates.

47 Responses to Why is JSON so popular? Developers want out of the syntax business.

  1. Simon 2011/08/11 at 8:52 am #

    totally agree. my only concern about json is the security implications of having data which can potentially contain executable code and that data needs to be executed in order to be accessed.

    this is a particular concern when you’re also using javascript in your back end. I can think of ways to protect against code injection on the server, but it’s still a worry ?

  2. Simon 2011/08/11 at 1:52 am #

    totally agree. my only concern about json is the security implications of having data which can potentially contain executable code and that data needs to be executed in order to be accessed.

    this is a particular concern when you’re also using javascript in your back end. I can think of ways to protect against code injection on the server, but it’s still a worry ?

  3. Neutral 2011/08/21 at 10:23 am #

    I disagree that JSON necessarily has “one straightforward way to represent something”

    what about

    {‘person’ : {‘name’: ‘{‘first’: ‘John’, ‘last’: “Smith”}}

    There are also libraries in Java that takes care of the processing for you. Using JAXB / Digester you don’t need to handle the DOM tree yourself.

  4. Neutral 2011/08/21 at 3:23 am #

    I disagree that JSON necessarily has “one straightforward way to represent something”

    what about

    {‘person’ : {‘name’: ‘{‘first’: ‘John’, ‘last’: “Smith”}}

    There are also libraries in Java that takes care of the processing for you. Using JAXB / Digester you don’t need to handle the DOM tree yourself.

    • Andy Edwards 2014/07/14 at 12:42 pm #

      Sure, but JSON will always be nicer than XML because {“person”: … } is more compact than … , and even greater savings are achieved with lists: Compare [“John Smith”, “Johanna Smith”, etc…] to John SmithJohanna Smith etc…

    • peter friedman 2014/07/15 at 2:48 pm #

      In the case of the XML representation as in the article, those are the exact same object structures, just written out differently (i.e. more work to parse but to get the exact same property and value pairs). Whereas in the case of the JSON example you’ve mentioned, you are comparing completely different objects / object structures. That’s an incorrect comparison. In the XML examples, no new information is relayed through the various representation. However, the JSON examples, you’ve now incorporated more structural/relationship information, hence why your JSON objects look different. That’s because they are different, the have more information packed in them.

  5. Daniele 2012/04/20 at 11:58 am #

    how long do you take to write an article or a post like this one?http://www.floresemesa.com

  6. radha 2013/09/16 at 12:36 am #

    while retrieving data from database how can we use json..can anyone explain please

  7. James Malvi 2014/09/25 at 7:45 am #

    Best tool I found to View , Analyse and edit JSON data. http://codebeautify.org/jsonviewer

    • teppie 2014/10/06 at 8:28 am #

      James Malvi a.k.a. codebeautify is on a comment-spamming spree. Please keep the honors to yourself, undo/remove your spam trail and stop misbehaving.

Trackbacks/Pingbacks

  1. Our Investment in MongoLab | Foundry Group - 2011/05/18

    […] Of the many offerings in the “Big Data” and NoSQL universe, we like the fact that MongoLab has chosen to specialize in 10gen’s MongoDB.  MongoDB’s scalability (in size and read/wriite volume), its ability to run MapReduce jobs and its accelerating adoption among developers are all compelling aspects of the MongoDB platform.  Equally important is the fact that MongoDB stores its data as native JSON objects which makes it very accessible to a wide swath of the development community.  MongoLab CEO Will Shulman blogs about this important aspect of MongoDB here. […]

  2. Investment » Blog Archive » Our Investment in MongoLab | Foundry Group - 2011/05/18

    […] Of a many offerings in a “Big Data” and NoSQL universe, we like a fact that MongoLab has selected to specialize in 10gen’s MongoDB.  MongoDB’s scalability (in distance and read/wriite volume), a ability to run MapReduce jobs and a accelerating adoption among developers are all constrained aspects of a MongoDB platform.  Equally vicious is a fact that MongoDB stores a information as local JSON objects that creates it really permitted to a far-reaching swath of a growth community.  MongoLab CEO Will Shulman blogs about this vicious aspect of MongoDB here. […]

  3. Good explanation on why developers prefer JSON | tivoliandme - 2011/06/22

    […] Source: http://blog.mongolab.com/?p=23 […]

  4. Why is JSON so popular? Developers want out of the syntax business. | MongoLab « The Ramblings of Ben - 2011/06/23

    […] Why is JSON so popular? Developers want out of the syntax business. By will on 2011/03/30 in JSON […]

  5. JSON is just easier to use than XML | Sean Walther’s Blog - 2011/06/23

    […] just easier to use than XML Thursday, June 23rd, 2011 | Business Saw a post at mongolab about JSON, Why is JSON so popular?  Developers want out of the syntax business.He’s right, JSON is popular not just because it’s “more efficient” at […]

  6. JASON vs XML - Kenneth Tucker - 2011/06/24

    […] Fri, Jun 24. 2011 Being an early proponent of XML and not Javascript I was not quick to warm to this article. However a number of good points are made. Chiefly among them being that the same bone head […]

  7. Web | Fragments - 2011/06/26

    […] Why is JSON so popular? Developers want out of the syntax business (mongolab.com) […]

  8. Link: Why is JSON so popular? « Tangled Helix - 2011/06/27

    […] Why is JSON so popular? Developers want out of the syntax business. | MongoLab […]

  9. Всем сотрудникам отдела! « - 2011/06/28

    […] 11. Почему JSON так популярен? […]

  10. JavaScript Magazine Blog for JSMag » Blog Archive » News roundup: Paper.js, Fathom.js, test262 - 2011/07/01

    […] Why is JSON so popular? Developers want out of the syntax business […]

  11. Weekly Links– 2010_24 (50 for Web Devs & Other Geeks) :Misfit Geek - 2011/07/15

    […] Why is JSON so popular? Developers want out of the syntax business. […]

  12. Recently in Web Development (July Edition) | FlexLib - 2011/07/22

    […] Read more […]

  13. Recently in Web Development (July Edition) - Furlogy.com - 2011/07/22

    […] Read more […]

  14. Recently in Web Development (July Edition) | Technology Story - 2011/07/22

    […] Read more […]

  15. Web Development articles, tutorials, help » Blog Archive » Recently in Web Development (July Edition) - 2011/07/22

    […] Read more […]

  16. Recently in Web Development (July Edition) | Shadowtek | Hosting and Design Solutions - 2011/07/22

    […] Read more […]

  17. Recently in Web Development (July Edition) | Website Design Tutorials & Guides - 2011/07/22

    […] Read more […]

  18. Web Development Company, Ecommerce News » Recently in Web Development (July Edition) - 2011/07/22

    […] Read more […]

  19. Recently in Web Development (July Edition) » abdie.web.id - 2011/07/23

    […] Read more […]

  20. Design Magazine » Recently in Web Development (July Edition) - 2011/07/23

    […] Read more […]

  21. Recently in Web Development (July Edition) - 2011/07/23

    […] Read more […]

  22. Recently in Web Development (July Edition) | Web Design Northamptonshire - 2011/07/24

    […] Read more […]

  23. Recently in Web Development (July Edition) « Leonardowesleidiniz's Blog - 2011/07/25

    […] Read more […]

  24. Recently in Web Development (July Edition) | Jetcat - 2011/07/25

    […] Read more […]

  25. Recently in Web Development (July Edition) | Allprowebdesigns.com - 2011/07/29

    […] Read more […]

  26. My Stream » Recently in Web Development (July Edition) - 2011/07/30

    […] Read more […]

  27. Recently in Web Development (July Edition) | Neil Kearney Design - 2011/08/03

    […] Read more […]

  28. Parser for XMLTV format and SchedulesDirect data « BCmoney MobileTV - 2011/10/27

    […] Why is JSON so popular? Developers want out of the syntax business (mongolab.com) […]

  29. Facts about Adipex-p | Health Insurance Benefits Today - 2012/01/15

    […] written many articles on Adipex and Adipex-P. More Discount Adipex Health Insurance Lead Articles Discount Adipex Health Insurance Lead Adipex-p is not pill that you can directly buy on internet. Y…ts who want to try Adipex-P need to go to their doctor for a complete medical history and physical […]

  30. harbaremnmn - 2012/05/02

    Blogs ou should be reading…

    […]Here is a Great Blog You Might Find Interesting that we Encourage You[…]……

  31. website - 2012/05/03

    You should check this out…

    […] Wonderful story, reckoned we could combine a few unrelated data, nevertheless really worth taking a look, whoa did one learn about Mid East has got more problerms as well […]……

  32. Have Fun On Your Vacation - 2012/05/05

    Have Fun On Your Vacation…

    […]Wonderful story, reckoned we could combine a handful of unrelated information, nevertheless really really worth taking a look, whoa did one particular find out about Mid East has got more problerms also […]…

  33. WordPress Freelance - 2012/05/08

    WordPress Freelance…

    […]check below, are some totally unrelated websites to ours, however, they are most trustworthy sources that we use[…]……

  34. wifi phones: PowerGen Dual Port USB 2.1A 10W AC Travel Wall Charger – White - 2013/02/25

    […] Original Post: http://blog.mongolab.com/2011/03/why-is-json-so-popular-developers-want-out-of-the-syntax-business/ […]

  35. cigarettes sale - buy online smokes - 2014/08/11

    […] my birth […]

Leave a Reply