by James 

Getting Up to Speed with JSON

0 Comments

JavaScript Object Notation (JSON) is a popular file format. It’s something that all data people should learn. It’s not complicated, which is good. But it supports nesting, which can cause some confusion for people. This article will help you in getting up to speed with JSON.

JSON on Screen

Why JSON?

The biggest advantage to JSON is its intuitiveness. At a glance, most people can determine what each data point represents. It is text-based, which means that most tools can read it easily. It also has widespread support with programming languages.

JSON has several uses including configurations, APIs, and data transfer. It’s even used as the core engine in MongoDB. If you know JSON, you already have a great base for knowing MongoDB.

The coverage of JSON in this article is not extensive. It serves to learn the basic structure of the language. I also will not cover any tools that process it.

Why Not XML?

XML, which stands for Extensible Markup Language, is another file format that is more formal than many other file formats. XML has its uses and is actually the superset of HTML (the language of the web).

The XML file format conforms to a strict standard that is usually overkill for many projects needing to interact with data. However, these strict standards come in handy for sensitive data or other applications that require scrutinizing the format of the data.

XML is not an easy standard for many to grasp and often requires tools that are not commonplace. JSON gives you several benefits that XML gives but is not strict on its standards. The use of either depends on the use cases presented.

JSON Basics

As mentioned, JSON is a text-based file format. It supports few data types, including strings, Boolean (true/false) and numbers. It also supports arrays and objects. In most cases, these types can be nested for more complex structures.

An object is similar to a Python dictionary for anyone familiar with the language. It contains a key/value pair surrounded by curly braces {}.

JSON documents are often identified by a key value pair. The key must be a string, but the value can be any of the types mentioned above. At its most basic level:

A valid JSON object can be as simple as:

{
               "name": "Alan",
               "age": 44
}

As mentioned earlier, objects use a key/value pair. In the above example, the two keys for the object are “name” and “age”. Note that no identifier exists for the collection of the name and age.

For instance, is the record for a customer or employee? One way to label this is as follows:

{
               "customers": {
                              "name": "Alan",
                              "age": 45
               }
}

With this structure, it’s clear that the person is part of the customer grouping. However, if you wanted to add another customer to the customers grouping, you would need to place the customers into an array. An array uses the square brackets:

{
               "customers": [
                              {
                                             "name": "Alan",
                                             "age": 45
                              },
                              {
                                             "name": "Nancy",
                                             "age": 42
                              }
               ]
}

Arrays can be used when you don’t have a label (like customers) to use but still want to group together like entities.

JSON is easy to grasp but the embedding aspects (hierarchy) can get confusing at first. You can use a validator (JSON) to determine if your JSON structures are correct.

https://jsonlint.com/

Using JSON

Several programming languages support reading and writing JSON, which seems at odds with what JSON stands for, i.e., JavaScript Object Notation. JavaScript does work well with JSON but it’s not the only choice. Both Python and R have libraries that support the format.

Next Steps…

You’ll find several resources online to learn more about JSON. I am reluctant to include any in this article because there are several methods of access and it’s best to search for the ones that you need for your projects. For instance, the W3 Schools website has great information on the format, but you’ll need to know JavaScript.

https://www.w3schools.com/js/js_json_intro.asp

Learn MongoDB to Learn JSON

MongoDB is a popular database engine. It supports a JSON-like construct. It’s actually BSON, which is a binary JSON. It’s rare for anyone working in MongoDB to access BSON directly. Essentially, when you work with MongoDB, you’ll be working in JSON even if under the covers it’s converted to BSON.

Web Database

The takeaway is if you install MongoDB on your machine (or use the Atlas cloud-based version), you can use the engine to create and read JSON files. The great aspect of this is MongoDB will validate your JSON for you. If you type something wrong, you’ll get a warning.

A side benefit of learning MongoDB is that it’s a wickedly marketable skill. Companies are looking for people who have a solid understanding of the technology.

About the author 

James

James is a data science writer who has several years' experience in writing and technology. He helps others who are trying to break into the technology field like data science. If this is something you've been trying to do, you've come to the right place. You'll find resources to help you accomplish this.

Leave a Reply

Your email address will not be published. Required fields are marked

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
Subscribe to get the latest updates