Skip to content

What is the JSON format?

  • Data

JSON is the abbreviation for the JavaScript Object Notation file format. It describes a standardized data format for storing data. It is one of the text file formats because the data is stored in human-readable, natural language.

How are these Files structured?

The structure of JSON files is, simply put, an unordered collection of key-value pairs. For Python programmers, this structure is best compared to the Python Dictionary.

# JavaScript Object Notation
{
    "city": "Berlin",
    "country": "Germany",
    "population": 3645000
}

The order of the keys “city”, “country” and “population” is not predefined, so a JSON file with a different order than that shown here will still be identical to the file displayed.

In JavaScript, objects can only have keys that are represented as strings. Since the JSON file format is derived from JavaScript, only strings can be used as keys.

The values within a pair can assume different data types. The following types are allowed:

  • String, e.g. “city”: “Berlin”.
  • Number, e.g. “population”: 3645000
  • Object, e.g. another JSON object
  • Array, e.g. “districts”: [“Kreuzberg”, “Pankow”, “Reinickendorf”]
  • Boolean, e.g. “capital”: True
  • Null

This wide variety of data types makes JSON a very popular file format. There are only a few exceptions, which cannot be stored as values in the JavaScript Object Notation.

In JSON no functions can be stored. At the same time, date formats cannot be stored natively. However, this is not a major problem, since you can store dates as strings and then convert them back to a date when reading out the file.

Advantages and Disadvantages of the JavaScript Object Notation

The use of such files is very popular due to their simplicity in structure. The requirements for the data structure are comparatively low and the files are quick and easy to understand for many users.

The widespread use can also be explained by the fact that there are now own parsers for all common programming languages, which make the use of JSON even easier.

The main drawbacks of this file format are the fuzzy number definition and the lack of standards for interpreting non-supported data types. The JavaScript Object Notation does not distinguish between common number formats, such as integer or decimal numbers. This makes the interpretation of numbers dependent on the implementation.

As already mentioned, there are some data types that are not supported by default, such as dates. This can be circumvented with the conversion to strings. However, there are different possibilities and libraries, which can be used for this and it was not agreed on any uniform standard.

Which Applications can be realized with JSON?

The JavaScript Object Notation is used in many different applications and programming languages. NoSQL and also various relational databases have connectors to store these file types.

In addition, it is also suitable for the following use cases:

  • Data transmission: When information is requested via an API, the JavaScript Object Notation is used as the response format in many cases.
  • Clear data storage: Due to the low data format requirements, flexible data structures can be stored easily.
  • Temporary data storage: The JavaScript Object Notation is also often used to store information in a program for a short time. Due to the high compatibility with other programming languages, these files can then also be used by different applications without further ado.

How to edit JSON files in Python?

Python has its own library for working with JavaScript Object Notation. The corresponding file must first be opened with “open”. The stored JSON object can then be converted into an array of Python dictionaries with “load”.

# Import JSON Module  
import json
  
# Open file
file = open('file.json')
  
# parse object in Python dictionary
data_dict = json.load(file)
  
# Close file
file.close()

# Iterate through object
for entry in data_dict: 
    print(entry)

Out: 
{"city": "Berlin", "country": "Germany", "population": 3645000}
{"city": "Paris", "country": "France", "population": 2161000}

From now on you can work with the data in the same way as you are already used to from Python dictionaries.

This is what you should take with you

  • JSON is the abbreviation for the JavaScript Object Notation file format.
  • It describes a standardized data format for storing data.
  • The file format is used for various applications because it is very easy to read and understand.
  • In addition, all common programming languages offer modules that simplify the work with JavaScript Object Notation.
  • The fuzzy number definition is the biggest disadvantage of using this file format.

Other Articles on the Topic of JSON

  • The formal standard for the ECMA JavaScript Object Notation can be found here.
close
Das Logo zeigt einen weißen Hintergrund den Namen "Data Basecamp" mit blauer Schrift. Im rechten unteren Eck wird eine Bergsilhouette in Blau gezeigt.

Don't miss new articles!

We do not send spam! Read everything in our Privacy Policy.

Cookie Consent with Real Cookie Banner