JSON

Share

What is JSON?

JSON is a simple programming language used to create configuration files and declare data for use in a program written in another, more complex programming language. It has .json as its typical file extension.

JSON is an abbreviation of JavaScript Object Notation. It's based on how objects are described in the JavaScript programming language, however, the JSON format isn't the same thing as JavaScript. JavaScript is designed for writing algorithms, while JSON is designed for declaring data that will be used by some algorithm.

Although data declared with JSON tends to be used by JavaScript programs, you can use JSON with any language, e.g. Python, PHP, etc., so long as you have a program that parses JSON written for that language (Python and PHP have JSON parsers built into their standards libraries).

Some JSON parsers don't support comments in JSON, even though JavaScript supports comments.

Example of JSON

For reference, an example of JSON code:

{
    "name": "John",
    "age": 42,
    "height": 170.2,
    "has a cat?": true
    "spouse": {
        "name": "Mary",
        "age": null,
    },
    "children": ["John Junior", "Mary Junior"]
}

As you can see, JSON is a very simple format. It supports strings, integers, floats, booleans, key-value maps, and lists.

JSON is preferred by many for two reasons. First, it's a lot more terse and simpler to parse than XML. Second, it lets you create nested structures like what you can do in XML that wouldn't be possible in simpler data formats like CSV. So it's a mix of performance and versatility.

Note: in order to consume JSON, a program still needs to parse its text code, so if you really need performance you shouldn't be using JSON, any text-based data format, you should be storing data as binary code optimized for your application. The reason why this isn't done is that you would need tooling to read this binary code later, and most programs don't really need that much performance, so it's easier to have configuration files as text files that you can trivially inspect and edit in any text editor.

By the way, in JavaScript, variables can be set and used like this:

var John = { "name": "John", "age": 42, /* etc. */ };
var age = John.age; // gets John's age;
var spouse_name_or_undefined = John.spouse?.name;
var cat_status = John["has a cat?"];

JSON doesn't have variables. JSON is just a way to declare the value.

Using JSON

To use JSON in a programming language, first you would need to load the JSON code into a string variable somehow, then give it to a JSON parser so it can convert the JSON code into a language-appropriate representation of the JSON code.

In Javascript this is easy.

// if we have the string client-side
const json_code = '{ "name": "John" }';
const json_as_object = JSON.parse(json_code);

// if we need to load it from the Internet
const response = await fetch("my-online-data.json");
const response_as_object = response.json();

In Python, there is no such thing as a Javascript object, so the equivalent would be a dict (dictionary) which is a mapping. Similarly, Javascript arrays become the equivalent in Python, list (lists).

In PHP, the array type is used for both integer-indexed arrays and string-indexed arrays (associative arrays).

Comments

Leave a Reply

Leave your thoughts! Required fields are marked *