JSON Schema Blog
Tags: webdev, beginners, programming, tutorial
Published: 2024-03-15 13:39:13

Hey Folks 👋, I am exciting to share something Great 🤯 here in this Blog!!  

I was working on my project </>, using an API and handling JSON, so i search something on google and literally i found something Crazy 🤩 thing so that is _JSON Schema_!! 

#### SO are you thinking 🤔 like what is JSON Schema ?? I had also the Same question 👀??
 
I also done a search on it and obtained information, but you don't have to search; I have already done it for you 😁.

##What is JSON Schema?

JSON Schema is a set of rules that you create for your JSON documents. These rules describe the structure, data types, and constraints of your JSON data, ensuring it meets certain expectations. It's like a blueprint for your JSON, making sure it's organized and correct.

JSON Schema is something like you can imagine organizing your LEGO bricks. It helps you to describe how your bricks should fit together to build something cool, like a spaceship or a castle. It provides a standardized way to define the properties, data types, and constraints within JSON data. 

It's like having a technical guidebook that enables developers to verify the integrity and correctness of their JSON data, similar to how engineers refer to technical specifications to ensure that their constructions meet required standards.

_**In One Sentence**_ :- JSON Schema is a JSON-based language for defining data structure and constraints, ensuring consistency and validity in your JSON data.


![JSON Schema img](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c6qkbua247ycxg3pqd2z.png)

## What are its benefits??

- It gives clear, human-readable, and machine-readable documentation.

- It ensure our data is in correct format & structure, which can help prevent errors and improve data quality.
 
- It can be used to document the structure of JSON data & help developers to understand the data and how it should be used.

- It can be used to establish a common language for data exchange, which can help simplify the process of sharing data between different applications and systems.

-  JSON Schema provides a standardized way to describe the structure of JSON data.

The benefits are endless.... The more you use it, the more you’ll discover. 


## Why JSON schema Validation required ?

- Using JSON Schema to define a model of your API response makes it easier to validate your API is returning the expected data.

- JSON Schema can be used to monitor your API responses, ensuring they adhere to a specified format.

- JSON Schema can help you get alerted when breaking changes occur in your API responses.


##How do you create your first JSON Schema 👍

Here's the code explaining how to create a schema.

```json
{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "id": "https://example.com/person.schema.json",
  "type": "object",
  "properties": {
    "name": {
      "type": "string"
    },
    "age": {
      "type": "integer"
    },
    "email": {
      "type": "string",
      "format": "email"
    },
  "required": ["name", "email"]
}
```

So, folks! If you are having a little confusion in the above JSON Schema structure, like what is $schema, type, properties, and all these things are, don't worry. The first time I saw it, I also had some confusion, but I referred to the table below, and these are different _**keywords**_ that define the functions of its use.

Let's the check various important keywords that can be used in this schema −

| Sr.no | Keyword & Description |
| ---------| -------:|
| 1 |  **$schema**  The $schema keyword states that this schema is written according to the draft 2020-12 specification. |
| 2 |  **type** The type keyword defines the first constraint on our JSON data: it has to be a JSON Object. |
| 3 |  **properties**  Defines various keys and their value types, minimum and maximum values to be used in JSON file. |
| 4 |  **required** This keeps a list of required properties. |


####Now, let's validate the JSON data using the JSON Schema!!

```json
{
  "name": "Aryan Gajjar",
  "age": 19,
  "email": "gajjararyan1509@gmail.com"
}
```
To validate this JSON data against the schema mentioned above, we can use a JSON schema validation library such as Ajv in JavaScript:


```js
const Ajv = require('ajv');
const ajv = new Ajv();
const schema = require('./example-schema.json');

const validate = ajv.compile(schema);

const jsonData = require('./example-data.json');
const isValid = validate(jsonData);

if (!isValid) {
    console.log(validate.errors);
} else {
    console.log('JSON data is valid!');
}
```



I think we can continue our discussion later from now, and we'll explore more in the next blog with more topics ,  I hope you've grasped the basics and feel a little curious to learn more. Until then, see you all! 👋

> Wait , wait a minute 😁 !!!


Till then keep understanding _JSON Schema_ more from here -> [JSON Schema](https://json-schema.org/) & follow there socials 😀 to stay connected 🤝 with them & to stay updated 🙌 !! 

####Here are some bonus ✨ points about YouTube tutorials 🤩 that, from my perspective, can enhance your understanding. I've come across these, and you might want to explore them further!

1. [JSON Schema in 60 seconds](https://youtu.be/JpzH8Eq663o?si=Q4kwB8BzZrN_bdzK)
2. [An Introduction to JSON Schema: A Practical Look](https://youtu.be/dtLl37W68g8?si=qCA4htpQk76B9ohj)
3. [What is JSON Schema](https://youtu.be/kK-_gL7Vsc0?si=VN4Z2pNLVJq-Bo36)
4. [Introduction to JSON Schema by Julian Berman](https://youtu.be/68jT0VGUsC4?si=5Yt68WOX91pUdKRh)

Thanks for reading my Blog, I think this Blog will be helpful for the all who are beginners to learn some technical knowledge and up skill themselves 🤗. Please share your Feedback's. Don't forget to share with your Developer Dude's 🤟.

> Thanks to JSON Schema Community 🧡 and Benjamin Sir 🤗 for giving this Opportunity to describe my words and help some folks to make aware about this Technology.....

You all can connect with me here 😎 :-  [LiNk To CoNnEcT](https://linktr.ee/aryangajjar)

Thank you ALL, Have a Great Day 🥳 !

![new](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05yrkisosjsfll1bb3b7.gif)