Today, we are going to find the answer of what is the structure of a YAML file and also check JSON in API Testing. So let’s start:
Table of Contents
API Testing
A backend developer provides the APIs to the frontend developer to use them in Mobile Apps. It is the responsibility of a front-end developer to test them upon receiving the same. For this purpose, a very effective tool of Postman is used. It is an API testing tool. Almost every front-end and back-end developer uses it. Though there are alternatives it is the most popular tool available in the business. Its URL is:
How Postman works
Postman is a fake API generation tool. Fake doesn’t mean any illegal activity but to check the veracity of APIs. We can do a live Website JSON test using it. The data we see on a website is in JSON.
How to check JSON on Postman
The first step is to sign up or log in using the GMAIL ID on Postman. After logging in, we go to Create Public Workspace. Select API Testing and then give it a name. Then in GET, we will paste the API link received from the backend developer. Now in real time, we can see our data in JSON. In these steps, we have simply learned the way of checking APIs. While integrating in the coming classes, we will explore it in detail.
What is the Structure of a YAML file?
YAML is a data structure or format. It is not a programming language. JSON is a serialization language and is more human-readable than JSON.
Should YAML lists be indented?
Yes, for sure. YAML emphasizes readability and is more sensitive than JSON. YAML works on indentation/ formatting. It is used in the configuration of files/projects of Flutter. YAML is a very sensitive language. If we forget to add a single space or even add an extra space, an error will instantly appear. So spacing is very important. We have to follow the format to do work on YAML. If we add two spaces then every key would need to be with two spaces.
Why to learn YAML
It is essential to learn YAML for a Mobile App Developer to work on pubspec.yaml file in Flutter. Pubspec.yaml is a configuration file of the Flutter Project in which we do all configurations. All third-party libraries as well as our Flutter project integration will be in this file.
How to write comments in YAML
We use a hashtag # for commenting in YAML. It is important to remember that we have to use it in each line to be declared as a comment. For example:
# This is a comment
# We are learning YAML
YAML Types
It has a very simple structure with the following two types:
- YAML Map
- YAML List
What is a map in XML?
It is similar to JSON Object. We add a Key, colon, and a space with value in the YAML map. We will use small alphabets with underscore for writing keys. Unlike JSON, we have the liberty to skip inverted commas in String values. Let’s see an example of a YAML map:
name: Ali
father_name: Usman
address: ABX
email: abc@gmail.com
roll_no: 699
gpa: 3.7
phone_num: null
is_married: false
YAML List
It is similar to JSON Array. We will write a hyphen (-), add space, and then give a value in the YAML list. For example:-
– Ali
– 45
– 3.23
– false
– null
Now we will see some Use Cases of YAML to fully understand it.
YAML list inside a Map
In this scenario, we will add a YAML list in a Map as under:
name: Ali
father_name: Usman
address: ABX
email: abc@gmail.com
roll_no: 699
gpa: 3.7
phone_num: null
is_married: false
subjects:
– Physics
– Chemistry
– Mathematics
– Computer
hobbies:
– cricket
– gardening
– traveling
Yaml Map inside a List
– Ali
– 45
– 3.45
– sub_1: Physics
– sub_2: Chemistry
– sub_3: Mathematics
– sub_4: Computer Science
How to test YAML
We can easily create, test, or compare JSON & YAML using the following website:
https://onlineyamltools.com/convert-yaml-to-json
Hopefully, now we understand what is the structure of a YAML file. We should practice it more and more in order to work on pubspec.yaml file of the Flutter project.