dwyl/learn-json-schema

What is a JSON Schema and why is it useful? How do I create/use a JSON Schema?

nelsonic opened this issue · 3 comments

Answer the question that is obviously on everyone's mind... 😉

JSON SCHEMA

JSON schema is a powerful tool for validating the structure of JSON data.

But what does it actually mean in layman's term?

What is JSON?

  • Javascript Object Notation
  • store information.
  • Uses following data structures
    • Object, Array, Number, String, Boolean, Null
{  
    “key1”: “value1”,
    “key2”: “value2”,
}

What is a JSON schema?

  • used to define the structure of the JSON Object.
  • useful tool for defining what type of data structure you want to return.

E.G.
When a user submits a form, you want to validate all the information they may have entered.

Expected Object:

{
    “type”: “object”,
    “properties”: {
        “first name”: { “type”: “string” },
        “last name”: { “type”: “string” },
        “age” : { “type”:  “number” }
    }
}

Now if the submit a the following object: ✖️ It would fail the validation that we have put in place.

{
    “first name”:”Mickey”,
    “last name”: “Mouse”
}

But, the following object: ✔️ It would pass the validation.

{
    “first name”:”Mickey”,
    “last name”: “Mouse”,
    “age”: 100
}

There we have our first JSON Schema for validation purposes! 😄

@sohilpandya edit "first name" to "last name" in the Expected Object as u have written it twice 😄

Thanks! @deadcoder0904 😄