What Is JSON

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 26

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 ?

 JSON stands for JavaScript Object Notation


 JSON is a lightweight format for storing and transporting data
 JSON is often used when data is sent from a server to a web
page
 JSON is "self-describing" and easy to understand
 JSON is a syntax for storing and exchanging data.
 JSON is text, written with JavaScript object notation.
JSON - Syntax

 JSON syntax is basically considered as a subset of JavaScript


syntax; it includes the following −
 Data is represented in name/value pairs.
 Curly braces hold objects and each name is followed by
':'(colon), the name/value pairs are separated by , (comma).
 Square brackets hold arrays and values are separated by ,
(comma).
 JSON supports the following two data structures −
Json - Syntax

1. Collection of name/value pairs − This Data Structure


is supported by different programming languages.

2. Ordered list of values − It includes array, list, vector


or sequence etc.
JSON Data -Types

 JSON format supports the following data types −

Sr.No. Type & Description

1 Number - double- precision floating-point format in JavaScript

2 String - double-quoted Unicode with backslash escaping

3 Boolean - true or false

4 Array - an ordered sequence of values


JSON Data - Types

5 Value - it can be a string, a number, true or false, null etc.

6 Object - an unordered collection of key : value pairs

7 Whitespace - can be used between any pair of tokens

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

Sr. Type & Description


No.
1 Integer - Digits 1-9, 0 and positive or negative

2 Fraction - Fractions like .3, .9

3 Exponent - Exponent like e, e+, e-, E, E+, E-


JSON Data - Types

 SYNTAX :
 var json-object-name = { string : number _ value, }

 STRING :

 It is a sequence of zero or more double quoted Unicode characters with backslash


escaping.
 Character is a single character string i.e. a string with length 1.
 The table shows various special characters that you can use in strings of a JSON
document −
JSON Data - Types

Sr. Type & Description


No.
1 " - double quotation

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

 It is an unordered set of name/value pairs.


 Objects are enclosed in curly braces that is, it starts with '{' and ends with
'}'.
 Each name is followed by ‘:'(colon) and the key/value pairs are separated by
, (comma).
 The keys must be strings and should be different from each other.
 Objects should be used when the key names are arbitrary strings.
 Syntax
 { string : 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.

 JSON Schema Validation Libraries :


 There are several validators currently available for different programming
languages. Currently the most complete and compliant JSON Schema validator
available is JSV.
JSON Schema Validation Libraries

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 −

Sr.No. Keyword & Description

1 $schema - The $schema keyword states that this schema is written according to the
draft v4 specification.

2 Title - You will use this to give a title to your schema.

3 Description - A little description of the schema.

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.

6 Required - This keeps a list of required properties.

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.

10 exclusiveMaximum - If "exclusiveMaximum" is present and has boolean value true, the


instance is valid if it is strictly lower than the value of "maximum".
Various important keywords that
can be used in this schema −

11 multipleOf - A numeric instance is valid against "multipleOf" if the result of the


division of the instance by this keyword's value is an integer.

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

Sr.No Method & description


1 get(String name)
This method just Returns the value but in the form of Object type
2 getBoolean(String name)
This method returns the boolean value specified by the key
3 getDouble(String name)
This method returns the double value specified by the key
4 getInt(String name)
This method returns the integer value specified by the key
5 getLong(String name)
This method returns the long value specified by the key

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.