DEV Community

Aryan Gajjar
Aryan Gajjar

Posted on

JSON Schema Blog

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

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.

{
  "$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"]
}
Enter fullscreen mode Exit fullscreen mode

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!!

{
  "name": "Aryan Gajjar",
  "age": 19,
  "email": "gajjararyan1509@gmail.com"
}
Enter fullscreen mode Exit fullscreen mode

To validate this JSON data against the schema mentioned above, we can use a JSON schema validation library such as Ajv in JavaScript:

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!');
}
Enter fullscreen mode Exit fullscreen mode

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 & 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
  2. An Introduction to JSON Schema: A Practical Look
  3. What is JSON Schema
  4. Introduction to JSON Schema by Julian Berman

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

Thank you ALL, Have a Great Day ๐Ÿฅณ !

new

Top comments (0)