Skip

What is WoT: JSON in practice

by the Web of Things Community Group

In the last video, we showed mostly primitive types of JSON. In this video, we will focus on first arrays and objects, and then we will mention common mistakes when writing JSON. At the end of the video, we will demonstrate the usage of JSON generation and parsing. The corresponding web page of this video is at https://w3c.github.io/wot-cg/tutorials/whatiswot/docs/preliminary/json/practice.

Skip

Video

Previous: JSON introduction Next: JSON Schema introduction

Transcript

In the last video, we showed mostly primitive types of JSON. In this video, we will first focus on arrays and objects, and then we will mention common mistakes when writing JSON.

At the end of the video, we will demonstrate the usage of JSON generation and parsing.

In JSON, each value in an array is called an item.

Arrays with items of different types are also allowed.

Nested objects are allowed in JSON. ‘measurement’ keyword is an example of this.

Objects start and end with curly brackets. It is basically defining keywords and mapping these keywords to values.

In this example, we define the name and value pairs to describe the data sent by sensors.

There is a JSON array of JSON Objects which means all items are JSON objects in that array.

Now we will talk about common mistakes while writing JSON files.

Let's start with the space character which is frequently overlooked.

Space is a character too.

So ‘is Active’ is not the same as ‘is Active blank’. Also, JSON is case sensitive. Thus, ‘is Active’ with upper case A is not the same as ‘is active‘ with lower case A.

Paying attention to this is important to make sure that object assignments and comparisons work.

Lets continue with the mistakes about quotation marks.

Forgetting quotation marks is wrong, it will show syntax errors everywhere.

Also putting them when you should not, causes unwanted results.

For example → "name":"true" is not the same as "name":true without quotation marks.

The first one is not a boolean but a string so it will be interpreted as a string.

The same problem happens with numbers too.

This way, the types will be incorrectly interpreted by your programming language.

Putting comments inside JSON documents is not valid.

Commas should not be used at the final name-value pair.

That is why the first one is not valid but difficult to see if you are using it inside of a javascript editor rather than a JSON editor.

Similar to the previous one depending on the programming language that you use, syntax might differ.

Since JSON will be the object of that programming language before it is parsed and serialized as JSON.

For example, in JSON key-value pairs have to be with quotation marks whereas in JavaScript files it is not necessary to have quotation marks, both work.

The syntax of the keywords might differ as well.

For example, true false keywords in JSON, Rust, JavaScript, and Golang are written lowercase, while in Python the first letter of these keywords needs to be capitalized.

Similar to the previous one, the null keyword differs in JSON and Python.

In JSON and JavaScript it is null with lowercase while in Python, the same meaning is given with the "None" keyword.

Now let's test JSON out in a real programming environment.

Open a code editor. In this example, we used VS Code.

Write a JSON file of your choice and save it by giving a file name and adding a .json extension.

Then the editor will recognize the file as a JSON document.

Now we will read a JSON file from the file system using a JSON library in Python.

We open the JSON file that we have previously saved to the file system with open-as-file key words.

Then, we can load that JSON file with the load function into the data object.

We can also print it to see what is inside the JSON file.

Printing the data object in console can look like this.

There are many ways to send a JSON file via different protocols, one of them is using Python and its request library for sending an HTTP request.

With this library, you can use the post function to send an HTTP POST request with a JSON Payload.

As a parameter, it takes the URL to send to and the JSON object.

You can proceed to the next tutorial by clicking on the card or the video description.

Previous: JSON introduction Next: JSON Schema introduction

Skip

Sponsors

Support TPAC 2024 and get great benefits from our Sponsorship packages.
For further details, contact [email protected]

Who are our sponsors?

Gold sponsor

AWS

Silver sponsor

Google

Bronze sponsor

Igalia