Data scientists use programming languages to analyse Big Data. One of the ways to represent data or to interchange data over different platforms and programming languages is JSON, which stands for JavaScript Object Notation. This article provides a brief introduction to JSON.
What is JavaScript?
Before we jump into JSON, let’s look at what JavaScript is.
JavaScript is a text-based programming language for making web pages interactive using elements that engage a user including search boxes, embedded videos and feed refreshes.
This language was initially created for browsers only. But now, it can also be used on the server or on any device that has a JavaScript engine.
The programmes in this language are called scripts and they can be written in a web page’s HTML and run automatically as the page loads.
Despite its name being so similar to Java (another programming language, not the Indonesian island), JavaScript is very different from Java.
One main difference is that JavaScript’s text-based nature, whereby the scripts are given as plain text, doesn’t need any special preparation.
Now that you know what JavaScript is, let’s look at what JSON is.
What is JSON and why do we need it?
JSON is a JavaScript-based lightweight text format for storing and transporting data, a process known as data interchange.
Put clearly, it is a way to store information in an organised, easy-to-access manner.
Due to its simple, “self-describing” and understandable nature, JSON is often used when data is sent from a server to a web page.
Several features make JSON an ideal data interchange language and a great alternative to the complicated Extensive Markup Language (XML).
JSON data is stored in .json files. They are plain text files that can easily be opened and used.
For many people, less is more and simplicity is king, especially in improving load times of web pages.
JSON data has become increasingly important for websites to load data faster, by using JavaScript in the web browser to load page content dynamically in the background and giving the user a faster, smoother experience.
Being able to switch the contents of a certain web page element without refreshing is convenient and impressive.
In addition to this, data can be encoded in JSON using any programming language. Commonly, there are libraries for all popular programming languages that make it even easier to generate and access JSON data.
So, programmers fluent in programming languages like C, C++, C#, Java, JavaScript, Perl, Python, etc will be comfortable using JSON.
How does JSON work?
JSON is built on two universal data structures:
- A collection of name/value pairs. In various languages, this is realised as an object, record, struct, dictionary, hash table, keyed list, or associative array.
- An ordered list of values. In most languages, this is realised as an array, vector, list, or sequence.
Based on these structures, here’s how JSON syntax works:
- JSON data is written as name/value pairs. A name/value pair consists of a field name in double quotes, followed by a colon, followed by a value in double quotes:
"firstName":"Dhanhyaa"
- Curly braces hold objects. Objects can contain multiple name/value pairs and are separated by commas:
{"firstName":"Dhanhyaa", "lastName":"MS"}
- Square brackets hold arrays (groups of objects). In this example, the object “employees” is an array since it contains three objects:
"employees":[ {"firstName":"Dhanhyaa", "lastName":"MS"}, {"firstName":"Tim", "lastName":"Frey"}, {"firstName":"Linh", "lastName":"Dang"} ]
If you want to play around with the JSON codes, you can try using these browser-based tools:
- JSON Editor Online
- Online Stack.hu JSON Viewer
- JSONLint
- Prettydiff JSON Beautifier
- JSON to CSV Converter
- JSON to XML Converter
WordPress RESTful JSON API
Here, we touch on what JSON can do on WordPress via the WordPress REST (or RESTful) API.
First things first, an API (application programming interface) allows multiple apps to interact with one another.
Meanwhile, a REST (representational state transfer) gives a set of guidelines for developers to use when building APIs that function effectively.
Hence, the REST API enables WordPress to interact and exchange data with just about any site and web app regardless of what languages an external programme uses.
It ties back to the whole web infrastructure design.
And what does JSON have to do with this?
JSON is the format this API uses to exchange data since JSON is able to connect apps that use different programming languages.
Using JSON, the REST API can receive and respond to certain types of requests using simple HTML commands.
For instance, a client may send HTTP GET along with an endpoint to retrieve a resource/data/article from the server.
An endpoint is the HTTP combined with a route which tells the API where to go and what data to interact with.
According to the REST API Handbook, this is how you can retrieve a post:
GET /wp/v2/posts/<id>
You can then query this endpoint to retrieve a specific post record:
$ curl https://yourwebsite.com/wp-json/wp/v2/posts/<id>
Alternatively, you can just paste the URL in a browser and then see the JSON data. For example, when you do this with the iunera Big Data Science blog, you get the following result:
{ "id": 11069, "date": "2020-11-12T02:00:00", "date_gmt": "2020-11-12T01:00:00", "guid": { "rendered": "https://www.iunera.com/?p=11069" }, "modified": "2020-11-11T04:01:50", "modified_gmt": "2020-11-11T03:01:50", "slug": "an-easy-guide-to-structured-unstructured-and-semi-structured-data", "status": "publish", "type": "post", "link": "https://www.iunera.com/kraken/big-data-science-intelligence/an-easy-guide-to-structured-unstructured-and-semi-structured-data/", "title": { "rendered": "An Easy Guide To Structured, Unstructured And Semi-Structured Data" }, "content": { "rendered": "", "protected": false }, "excerpt": { "rendered": "<p>In every business or organization, Data is fundamental to its decisions. A company’s ability to gather the right data, interpret it, and act on those insights is often what will decide its level of success</p>\n", "protected": false }, "author": 10, "featured_media": 11240, "comment_status": "open", "ping_status": "open", "sticky": false, "template": "", "format": "standard", "meta": { "ub_ctt_via": "" }, "categories": [ 28, 61, 36, 35, 37 ], "tags": [ 6, 237, 51, 203 ] }
Recap
JSON derives its simplicity from JavaScript’s text-based approach to making web pages more interactive.
JSON’s ease of use and flexibility to write it in any programming language make it a great data interchange language.
That’s why it is used commonly in Data Science and Big Data projects nowadays.
You’ll find out more about why JSON is useful in our next article about JSON-LD (JSON for Linking Data).
With that said, it’s easier to know JaSON first before meeting his LaDy (JSON-LD), right?