Categories
JSON

Working with JSON — Data Types and Schemas

Spread the love

JSON stands for JavaScript Object Notation.

It’s a popular data-interchange format that has many uses.

In this article, we’ll take a look at how to use JSON.

JSON Number Data Type

Numbers are supported with JSON.

We just add them as values.

For example, we can write:

{
    "latitude": 49.606209,
    "longitude": -122.332071
}

to add the numbers as the values.

We have decimal numbers in the JSON. We can also have integers.

JSON Boolean Data Type

JSON objects can also have boolean values. Boolean can have values true or false .

For example, we can write:

{
    "toastWithBreakfast": false,
    "breadWithLunch": true
}

to add them as values.

JSON Null Data Type

The null data type can have the value null .

It is used to represent nothing.

For example, we can write:

{
    "freckleCount": 1,
    "hairy": false,
    "color": null
}

Then we set the value of color to null , which means that no color is set.

JSON Array Data Type

JSON supports array for storing collections of data.

For example, we can write:

{
    "fruits": [
        "apple",
        "orange",
        "grape"
    ]
}

to add the fruits property to an JSON and set the value to an array with the square brackets enclosing the values.

We can add null to an array to add empty values:

{
    "fruits": [
        "apple",
        "orange",
        null,
        "grape"
    ]
}

We can mix and match data types in JSON arrays just like JavaScript arrays.

JSON arrays can have any JSON supported data types in an object.

So we can write:

{
    "scores": [
        92.5,
        62.7,
        84.6,
        92
    ]
}

or

{
    "answers": [
        true,
        false,
        false,
        true,
        false,
        true,
        true
    ]
}

or

{
    "students": [
        "james smith",
        "bob jones",
        "jane smith"
    ]
}

Arrays can also have arrays in them.

For example, we can write:

{
    "tests": [
        [
            true,
            false,
            false
        ],
        [
            true,
            true,
            false
        ],
        [
            true,
            false,
            true
        ]
    ]
}

We add the booleans in an array and then put the arrays in another array outside of it.

JSON Schema

A JSON schema lets us check our JSON for the structure that we expect.

We can validate the conformity of our data with the schema and fix errors that are found.

Validation errors let us fix errors that are found.

This makes us feel confident about the data.

To add a JSON schema to our object, we can write:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Person",
    "properties": {
        "name": {
            "type": "string"
        },
        "age": {
            "type": "number",
            "description": "Your person's age in years."
        },
        "gender": {
            "type": "string"
        }
    }
}

We defined the schema object with the $schema property.

Then we have the title to add the title to the schema.

Then we have the properties object with the properties that are allowed in the JSON object that we’re checking against this schema with.

For example, if we have:

{
    "name": "james",
    "age": 20,
    "gender": "male"
}

then that conforms to our schema.

Conclusion

JSON lets us use various data types in our JSON objects.

Also, we can create JSON schemas to validate objects.

Leave a Reply

Your email address will not be published. Required fields are marked *