DEV Community

aohibbard
aohibbard

Posted on

What exactly is JSON?

During a recent interview, I was tasked with working with a JSON object in the language of my choice. While I'll skip over the the particularities of the situation, I realized that I had some uncertainty over what exactly JSON is. I've worked with JSON extensively in JavaScript concepts, and I've utilized methods like JSON.parse() and JSON.stringify(), knowing that these are valuable in managing the data and sending it between the front and back end. But there was a conceptual foundation missing from my practical usage. Let's start with a an official definition from Mozilla:

JavaScript Object Notation (JSON) is a standard text-based format for representing structured data based on JavaScript object syntax. It is commonly used for transmitting data in web applications (e.g., sending some data from the server to the client, so it can be displayed on a web page, or vice versa).

If we start with just the name, it is the second half that really stands out. JSON is an object notation, or as Mozilla says, a "text-based format" for representing data. This of course makes sense. If we are utilizing a back-end such as Rails or Django, it does not make much sense that our data would be circulating as a JavaScript object.

So JSON is in fact a text-based notation (think XML, YAML) that format data into a readable object format for JavaScript. This is why it is import to run JSON.parse() when data enters the a JavaScript front end: because it is coming through as a string, albeit one that is formatted as a JavaScript object. If we were just sending a really long string this would be, well, a pain. But of course.

Alan Rickman as Severus Snape in a gloomy dungeon saying mouth "Obviously." "Obviously" is the caption in the image.

Conventional wisdom suggests that, with the rise of JavaScript, JSON took the throne from XML as a preferred way to send data in APIs given its easier readability and accessibility as a JavaScript object. Indeed, working with JSON is just as easy as working with JavaScript objects because (if parsed), JSON is an JavaScript object. Most histories date JSON back to 1999 or 2000 and it began to pick up usage in the aughts, but really grew in the last decade particularly when it became an ECMA international standard in 2013. (For some context on JSON vs. XML, this post has a nice graph, with some highlights being that JSON is easier to read and parse, though has some more security vulnerability.)

What about generating JSON outside of JavaScript? The standardization of JSON has made it very easy to work with and many languages have native support for working with JSON. In Python, you simply need to import json and there are plenty of functions available to parse JSON data and to format data into JSON. There are also libraries like Jackson and GSON in Java. And frameworks have plenty of support too: Rails, for example, has the Active Model Serializer, although I'm a big fan of Fast_JSON API as it offers a faster performance.

JSON is not a difficult concept to understand. But it is always a good reminder to think through the basic concepts of what you are working with.

Top comments (0)