ADVERTISEMENT
ADVERTISEMENT

Home » JSON

Data Types in JSON

JSON Data Types: Here, we are going to learn about the various data types in JSON with examples.
Submitted by Siddhant Verma, on November 11, 2019

We know that we use JSON as a mechanism and a medium to transport data on the web. Inside the key-value pairs, the JSON data has a predefined schema which tells us what types of data these values can manifest. In this article, we'll look at some common data types in JSON.

Primary Types

JSON data can assume the values to be any of the primary data types such as a string, number, integer, boolean constants or null. These can be in the form of the ordered or unordered list too in the form of objects and arrays but we'll explore them separately later. JSON Documents can be either basic values (strings, numbers, integers, the boolean constants or null). Thus the four basic value types supported by JSON Schema are,

  • String
  • Integer
  • Boolean
  • NULL

JSON String

The values in JSON data can be strings or simply a set of characters. We can also specify the entire document to have only the string schema too by writing,

    {"type": "string"}

All the characters must be enclosed inside double quotes and should be logically interpreted as a string to have the string schema,

    {"id": "101"}

The above JSON specified id to be a string but does not satisfy the string schema. Similarly,

    {"flag": "true"}

It also specifies to be a string but yet again does not satisfy the string schema.

    {"name": "Ryan"}

Satisfied the string schema completely. There are certain restrictions we can impose on the string value. For instance, we specify the minimum and maximum length of our string,

{
  "type": "string", 
  "minLength": 3,
  "maxLength": 7
}

The above schema uses some string restrictions in JSON.

JSON Number

Another form of value in JSON are numbers. For example,

    {"age": 45}

In JSON Schema we can also specify that a document must be a number by writing,

    {"type": "number"}

Again as previously in a JSON Schema, the numeric value must only be a number and not a floating-point. However, inside the document, the value can be anything as long as it satisfies any one of the below-mentioned rules,

  1. It is represented in base 10 with no superfluous leading zeros.
  2. Digits are between 0 and 9.
  3. It's a negative number or a fraction.
  4. It's an exponent of 10 prefixed by e.
{
  "pages" : 210,
  "temperature_kelvin" : -210,
  "temperature_celcius" : 21.05,
  "num" : 1.0E+2
}

The above JSON gives an example of the various kinds of numbers we can have. We can also specify the JSON schema to be an integer,

    {"type": "integer"}

We can also have restrictions while using numbers in a JSON schema using maximum and minimum properties.

JSON Booleans

When a JSON value is a boolean it is either true or false without any quotes. If we put true and false within quotes it will be treated as a string.

    {"isPrime": true}

We can also specify the type to be Boolean inside a JSON schema,

    {"type": "Boolean"}

JSON Null

Values in JSON can also be null if they are expected to be empty.

    {"salary": null}

JSON values cannot be a function, date or undefined. We can use these data types to create JSON data, convert JSON data from one type to another and also generate JSON Schemas especially in a Nosequel type of databases like MongoDB, firebase, etc. We'll explore two more data types in JSON- arrays and objects separately.

ADVERTISEMENT



ADVERTISEMENT



Comments and Discussions


ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.