What Is JSON
What Is JSON
What Is JSON
• JSON SYNTAX
• JSON DATA-TYPES
• JSON SCHEMA
• JSON OBJECTS
• JSON SERIALIZING
• JSON PARSING
• ADVANTAGES OF JSON
• DISADVANTAGES OF JSON
What is JSON ?
8 Null – empty
JSON Data - Types
Number
Itis a double precision floating-point format in
JavaScript and it depends on implementation.
Octal and hexadecimal formats are not used.
No NaN or Infinity is used in Number. The following
table shows the number types −
JSON Data - Types
SYNTAX :
var json-object-name = { string : number _ value, }
STRING :
2 \ - backslash
3 / - forward slash
4 b - backspace
5 f - form feed
6 n - new line
7 r - carriage return
8 t - horizontal tab
9 u - four hexadecimal digits
JSON Data - Types
Syntax :
var json-object-name = { string : "string value", }
Boolean :
It includes true or false values.
Syntax :
var json-object-name = { string : true/false, }
JSON Data - Types
Array :
It is an ordered collection of values.
These are enclosed in square brackets which means that array begins with .[. and ends with .]..
The values are separated by , (comma).
Array indexing can be started at 0 or 1.
Arrays should be used when the key names are sequential integers.
Syntax :
[ value, ]
JSON Data - Types
Whitespace
It can be inserted between any pair of tokens. It can be added
to make a code more readable.
Syntax
{string:" ",. }
JSON - Objects
JSON objects can be created with JavaScript. Let us see the various ways of
creating JSON objects using JavaScript −
Creation of an empty Object −
var JSON Obj = {};
Creation of a new Object −
var JSON Obj = new Object();
Creation of an object with attribute book-name with value in string,
attribute price with numeric value. Attribute is accessed by using '.'
Operator −
var JSON Obj = { "book-name” : “ VB BLACK BOOK", "price":500 };
JSON – Schema :
JSON Schema is a specification for JSON based format for defining the structure
of JSON data.
It was written under IETF draft which expired in 2011.
JSON Schema − Describes your existing data format.
Clear, human- and machine-readable documentation.
Languages Libraries
C WJElement (LGPLv3)
Java json-schema-validator (LGPLv3)
.NET Json.NET (MIT)
ActionScript 3 Frigga (MIT)
Haskell aeson-schema (MIT)
Python Jsonschema
Ruby autoparse (ASL 2.0); ruby-jsonschema (MIT)
PHP php-json-schema (MIT). json-schema (Berkeley)
Orderly (BSD); JSV; json-schema; Matic (MIT); Dojo; Persevere (modified BSD or
JavaScript AFL 2.0); schema.js.
Various important keywords that
can be used in this schema −
1 $schema - The $schema keyword states that this schema is written according to the
draft v4 specification.
4 Type - The type keyword defines the first constraint on our JSON data: it has to be a
JSON Object.
Various important keywords that
can be used in this schema −
5 Properties - Defines various keys and their value types, minimum and maximum values to be
used in JSON file.
7 Minimum - This is the constraint to be put on the value and represents minimum acceptable
value.
8 exclusiveMinimum - If "exclusiveMinimum" is present and has boolean value true, the instance
is valid if it is strictly greater than the value of "minimum".
9 Maximum - This is the constraint to be put on the value and represents maximum acceptable
value.
12 maxLength - The length of a string instance is defined as the maximum number of its
characters.
13 minLength - The length of a string instance is defined as the minimum number of its
characters.
14 Pattern - A string instance is considered valid if the regular expression matches the
instance successfully.
Json Serializing
For more control over how an object is serialized, the JsonSerializer can be used
directly.
The JsonSerializer is able to read and write JSON text directly to a stream via
JsonTextReader and JsonTextWriter.
Other kinds of JsonWriters can also be used, such
as JTokenReader/JTokenWriter, to convert your object to and from LINQ to JSON
objects, or BsonReader/BsonWriter, to convert to and from BSON.
JsonSerializer has a number of properties on it to customize how it serializes JSON.
These can also be used with the methods on JsonConvert via the
JsonSerializerSettings overloads.
JSON - Parsing
For parsing a JSON object, we will create an object of class JSON Object and
specify a string containing JSON data to it.
Its syntax is − String in;
JSON Object reader = new JSONObject(in);
The last step is to parse the JSON.
A JSON file consist of different object with different key/value pair etc.
So JSON Object has a separate function for parsing each of the component of
JSON file.
Apart from these methods, there are other methods provided by this class for
better parsing JSON files.
These methods are listed below −
JSON - Parsing
6 length()
This method returns the number of name/value mappings in this object..
7 names()
This method returns an array containing the string names in this object.
Advantage of JSON -
JSON is Faster :
JSON syntax is very easy to use. We have to use only as an syntax which provide us
a easy parsing of the data and faster execution of the data.
Since its syntax is very small and light weighted that's the reason that it executes
the response in faster way.
Server Parsing :
On the server side parsing is the important part that developers want if the parsing
will be fast on the server side then only user can get the fast response of their response
so in this case JSON server side parsing is strong point that indicates us to use the
JSON on server side .
Advantage of JSON -
Schema Support :
It has wide range of supported browser compatibility with the operating systems so the
applications made with the coding of JSON doesn't require much effort to make it all
browser compatible.
During development developer thinks for the different browsers but JSON provides that
functionality.
Tool for sharing data :
JSON is the best tool for the sharing data of any size even audio, video etc.
This is because JSON stores the data in the arrays so data transfer makes easier.
For this reason, JSON is a superior file format for web APIs and for web development.
Disadvantages of JSON -
First and foremost, in JSON has no error handling for JSON calls.
Another major drawback of JSON is that it can be quite dangerous if used with
untrusted services or untrusted browsers, because a JSON service returns a JSON
response wrapped in a function call, which will be executed by the browser if it will
be used with untrusted browser it can be hacked, this makes the hosting Web
Application Vulnerable to a variety of attacks.
If you are going to use JSON services, it's very important to be aware of the threats
which JSON have in that and also be aware with the things which can protect it.
JSON only have limited supported tools which we can use during JSON
development.