JSON - An Introduction with Python

JSON - An Introduction with Python

JSON: An Introduction with Python

JSON Nov 10, 2022

JSON or JavaScript Object Notation is a text-based data interchange format based on JavaScript. However, it can be used in any programming language. Douglas Crockford created the format.

JSON is utilised in the REST API. You may also use XML as a substitute, although developers prefer JSON since it is more readable and lighter.

How JSON works

The following can be used as values ​​in JSON:

  • numbers;
  • lines;
  • arrays;
  • JSON objects;
  • literals (boolean values ​​true, false, and null).

There will be no trouble with basic values. Let's look at arrays and JSON objects because we'll be working with them.


JSON object

A JSON object is an unordered set of pairs wrapped in curly braces, and dealing with it is similar to using a dictionary. "key:value"{ }

The key is the name of the parameter (property) that we give to the server. It acts as a marker for the system receiving the request, allowing it to comprehend what we have supplied it.

Consider the following example:

{
    "name": "Deep",
    "age": 26
}

We supplied a value "Deep" with a key "name"; to acquire this property, you must access it by its key; otherwise, this would be impossible.

The value might be more than simply a text or a number. It may be something else! Or an array, or an object within an array, or an array within an object... There can be an infinite number of nesting layers!

If we're talking about parameters, only strings are included in quotes; everything else is not.

Line breaks are optional. In general, spaces and hyphens are just required for human reading; the algorithm will comprehend without them.

For example, we may write a JSON object in this manner, and it will also be correct.

{"name": "Deep","age": 26}

The key is always a string, and we always quote it.

Because a JSON object is an unordered collection of  "key:value", the keys can be written in any order.

What is the correct way to say "name"? Python provides two methods for accomplishing this:

  1. Get the value using the .get()
  2. Get value by key.

get() vs dict[key]

The difference between these options is that if you try to access a key that does not exist, you will receive the KeyError .

profile = {
  "name": "Deep",
  "age": 26
}
print(profile[“first_name”])
>>> KeyError Traceback (most recent call last)
>>> KeyError: 'first_name'

However, if you use the get() method, you will get an empty value:

first_name = profile.get("first_name")
print(type("first_name"))
>>> NoneType

This method also includes a second passed parameter, which is used if there is no key:

first_name = profile.get("first_name", "There is no first_name")
print(first_name)
>>> 'There is no first_name'

JSON array

The array is enclosed in square brackets.

["GIRL", "BOY"]

A series of values separated by commas is enclosed in square brackets. Because there are no keys in this array, it can only be accessed by element number. As a result, in the case of an array, swapping data inside is impossible. Because it is an ordered set of values, the order is critical.

Values

An array can contain any value:

  • Numbers;
[40, 67, 21, 43]

  • strings;
["GIRL", "BOY"]

  • Literals;
[true, false]

  • Arrays;
["GIRL", "BOY", [40, 67, 21, 43]]

  • Objects;
[1, {p:1, q:2}, "Deep"]

  • Mixture.
[1, "Deep", false,  [40, 67, 21, 43], {"p": 1, "q": 2}]

Well Formed JSON

Rules for well formed json are as following:

  1. Data is written as key:value pairs
  2. Data separated by commas
  3. Object is inside curly braces{ }
  4. Array - inside square[ ]

I hope that this information was helpful. Thanks!


Tags

Anurag Deep

Logical by Mind, Creative by Heart