What is the JSON file format

JSON in 5 minutes: what is it? Easily explained


What you with JSON You can find out what it has in common with a TO-DO list here.

What do you need JSON for?

JSON (JavaS.cript Object Notation) is a Description language, that means you can save information with this language and send it from computer to computer.

Imagine the following: You get up in the morning, go to the bathroom and a note is stuck to the mirror.

You can tell immediately that it is a list of tasks for you. It's a TO-DO list for you.

The format, special characters and the heading quickly let you understand that you have something to do here.

So this list describes what you have to do and that seems like a lot of stuff!

What does JSON have to do with it?

Let's say you are fed up with writing yourself little pieces of paper every day. You want to build an app in which you can see and tick off your TO-DO's every day.

The daily TO-DOs should then be saved. Let's take your TO-DO list and try to put it into a program.

You can try to scan these in, but then you still can't get rid of the pieces of paper. So it would be easier to save the information in a file.

Of course you can just use a Word file here, but that would make programming unnecessarily complex. You'd first have to understand how Word stores and reads these files in order to use this format in your app.

Todo list in a text file

So let's take something simpler, a simple text file. We now write all TO-DO's there.

When your app reads the file, they will be displayed. If you tick a todo in the app, this change will be saved back to the file.

Unfortunately, there is still a catch here too: You just thought up the format of your text file! This means that you have to program the functions that allow your program to read and write the contents of the file in the correct format. In addition, the invented format is not a standard format.

That means you and your app are the only ones who can understand the whole thing. Apart from all the mistakes you made by mistake.

JSON to the rescue!

JSON is a standard format (ECMA-404) with which you can easily describe the information for your app.

The cool thing about it: The most common programming languages ​​such as JavaScript already have functions for writing and reading JSON.

// All TODO data var todoDaten; // Load data from text function readTodoDaten (text) {// Text - & gt; JSON var jsonObject = JSON.parse (text); todoData = jsonObject; } // store data in text function writeTodoData () {// JSON - & gt; Text var text = JSON.stringify (todoDaten); return text; }

To a JSON object to create from a text, we use the function JSON.parse and to write a JSON object in a text we use JSON.stringify. The whole thing looks similar in other programming languages. JSON has established itself as the standard, which means that there are many providers on the Internet who provide their information in JSON format.

For example Google Calendar API, Google Drive, Google Maps, Facebook API, Twitter, Youtube, and many more, as seen here: https://github.com/toddmotto/public-apis and https: // any- api.com/

JSON syntax

We have now found the way how and why you can work with JSON, but how is this JSON actually structured? Let's take a closer look at the TO-DO list. First we have a kind of heading and below that a list. Each entry in the list represents a task. This task consists of a name and a box.

On the paper we would draw a tick at the point as soon as the task is done. We have to describe this differently in the file. So let's write first

  • DONE YESor
  • DONE NO into it.

Our object consists of a property TODO with a list. Objects are described in JSON with curly brackets. A task in our TO-DO list is also an object. Objects have properties that can be given names. In our example it is the property todos or the property name as the name of the task. Perhaps you will find another property in our example? Properties always have a value.

With the property Name it is the name of the task as text, with the property Done it is the value whether the task has already been completed.

That would be very simple values. Properties and values ​​are always separated by a colon. Values ​​of these properties can also be separate objects or even lists. Lists are described with square brackets. Example: The 'todos' property consists of a list of objects.

If you have a list of objects or properties, you can separate them with a comma. And so you know the complete syntax of JSON.

Summary on JSON

As a brief overview, let's look again at our TODO list. You can find out how to program this in the link to this video. We see the curly braces that describe the whole JSON object. This object has a property TODO which has a list as its value.

This list has entries in the form of further objects. Each object in this list has two properties: The name of the task and a marker whether this task has already been completed. A little hint: Texts should always be packed in single quotes and truth values ​​can be described with true and false. The indentation and line breaks are also optional. They only serve as an overview, because JSON is very difficult to read as a person. With this you can already tick off the first point on your list. Feels good, doesn't it?

That was a JSON statement. For questions and suggestions, please use the comment function or simply write me an email.

Have fun programming and see you soon on lerneProgrammieren.de