SlideShare a Scribd company logo
Json tutorial
JSON
i
AbouttheTutorial
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for
human-readable data interchange. The JSON format was originally specified by Douglas
Crockford, and is described in RFC 4627. The official Internet media type for JSON is
application/json. The JSON filename extension is .json.
This tutorial will help you understand JSON and its use within various programming
languages such as PHP, PERL, Python, Ruby, Java, etc.
Audience
This tutorial has been designed to help beginners understand the basic functionality of
JavaScript Object Notation (JSON) to develop the data interchange format. After
completing this tutorial, you will have a good understanding of JSON and how to use it
with JavaScript, Ajax, Perl, etc.
Prerequisites
Before proceeding with this tutorial, you should have a basic understanding of the web
application’s work over HTTP and we assume that you have a basic knowledge of
JavaScript.
Copyright&Disclaimer
 Copyright 2017 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
JSON
ii
TableofContents
About the Tutorial....................................................................................................................................i
Audience..................................................................................................................................................i
Prerequisites............................................................................................................................................i
Copyright & Disclaimer.............................................................................................................................i
Table of Contents....................................................................................................................................ii
1. JSON ─ OVERVIEW...............................................................................................................1
Uses of JSON ...........................................................................................................................................1
Characteristics of JSON............................................................................................................................1
Simple Example in JSON..........................................................................................................................1
2. JSON ─ SYNTAX....................................................................................................................4
3. JSON ─ DATATYPES..............................................................................................................5
Number...................................................................................................................................................5
String ......................................................................................................................................................6
Boolean...................................................................................................................................................7
Array.......................................................................................................................................................7
Object .....................................................................................................................................................8
Whitespace .............................................................................................................................................8
null..........................................................................................................................................................9
JSON Value..............................................................................................................................................9
4. JSON ─ OBJECTS.................................................................................................................10
Creating Simple Objects ........................................................................................................................10
Creating Array Objects ..........................................................................................................................11
JSON
iii
5. JSON ─ SCHEMA ................................................................................................................13
JSON Schema Validation Libraries .........................................................................................................13
JSON Schema Example ..........................................................................................................................14
6. JSON ─ COMPARISON WITH XML.......................................................................................17
7. JSON ─ JSON WITH PHP.....................................................................................................18
Environment .........................................................................................................................................18
JSON Functions......................................................................................................................................18
Encoding JSON in PHP (json_encode) ....................................................................................................18
Decoding JSON in PHP (json_decode)....................................................................................................19
8. JSON ─ JSON WITH PERL....................................................................................................21
Environment .........................................................................................................................................21
JSON Functions......................................................................................................................................21
Encoding JSON in Perl (encode_json) ....................................................................................................22
Decoding JSON in Perl (decode_json) ....................................................................................................23
9. JSON ─ JSON WITH PYTHON ..............................................................................................25
Environment .........................................................................................................................................25
JSON Functions......................................................................................................................................25
Encoding JSON in Python (encode)........................................................................................................25
Decoding JSON in Python (decode) .......................................................................................................26
10. JSON ─ JSON WITH RUBY...................................................................................................27
Environment .........................................................................................................................................27
Parsing JSON using Ruby .......................................................................................................................27
JSON
iv
11. JSON ─ JSON WITH JAVA....................................................................................................29
Environment .........................................................................................................................................29
Mapping between JSON and Java entities.............................................................................................29
Encoding JSON in Java...........................................................................................................................30
Decoding JSON in Java...........................................................................................................................31
12. JSON ─ JSON WITH AJAX....................................................................................................33
JSON
5
JSON or JavaScript Object Notation is a lightweight text-based open standard designed for
human-readable data interchange. Conventions used by JSON are known to programmers,
which include C, C++, Java, Python, Perl, etc.
 JSON stands for JavaScript Object Notation.
 The format was specified by Douglas Crockford.
 It was designed for human-readable data interchange.
 It has been extended from the JavaScript scripting language.
 The filename extension is .json.
 JSON Internet Media type is application/json.
 The Uniform Type Identifier is public.json.
UsesofJSON
 It is used while writing JavaScript based applications that includes browser extensions
and websites.
 JSON format is used for serializing and transmitting structured data over network
connection.
 It is primarily used to transmit data between a server and web applications.
 Web services and APIs use JSON format to provide public data.
 It can be used with modern programming languages.
CharacteristicsofJSON
 JSON is easy to read and write.
 It is a lightweight text-based interchange format.
 JSON is language independent.
SimpleExampleinJSON
The following example shows how to use JSON to store information related to books based
on their topic and edition.
1. JSON ─ OVERVIEW
JSON
6
{
"book": [
{
"id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id":"07",
"language": "C++",
"edition": "second"
"author": "E.Balagurusamy"
}]
}
After understanding the above program, we will try another example. Let's save the below
code as json.htm:
<html>
<head>
<title>JSON example</title>
<script language="javascript" >
var object1 = { "language" : "Java", "author" : "herbert schildt" };
document.write("<h1>JSON with JavaScript example</h1>");
document.write("<br>");
document.write("<h3>Language = " + object1.language+"</h3>");
document.write("<h3>Author = " + object1.author+"</h3>");
var object2 = { "language" : "C++", "author" : "E-Balagurusamy" };
document.write("<br>");
document.write("<h3>Language = " + object2.language+"</h3>");
JSON
7
document.write("<h3>Author = " + object2.author+"</h3>");
document.write("<hr />");
document.write(object2.language + " programming language can be studied " +
"from book written by " + object2.author);
document.write("<hr />");
</script>
</head>
<body>
</body>
</html>
Now let's try to open json.htm using IE or any other javascript enabled browser that produces
the following result:
You can refer to JSON Objects chapter for more information on JSON objects.
JSON
8
Let's have a quick look at the basic syntax of JSON. 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).
Below is a simple example:
{
"book": [
{
"id":"01",
"language": "Java",
"edition": "third",
"author": "Herbert Schildt"
},
{
"id":"07",
"language": "C++",
"edition": "second"
"author": "E.Balagurusamy"
}]
}
JSON supports the following two data structures:
 Collection of name/value pairs: This Data Structure is supported by different
programming languages.
 Ordered list of values: It includes array, list, vector or sequence etc.
2. JSON ─ SYNTAX
JSON
9
JSON format supports the following data types:
Type Description
Number double- precision floating-point format in JavaScript
String double-quoted Unicode with backslash escaping
Boolean true or false
Array an ordered sequence of values
Value it can be a string, a number, true or false, null etc
Object an unordered collection of key:value pairs
Whitespace can be used between any pair of tokens
null empty
Number
 It is 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:
Type Description
3. JSON ─ DATATYPES
JSON
10
Integer Digits 1-9, 0 and positive or negative
Fraction Fractions like .3, .9
Exponent Exponent like e, e+, e-,E, E+, E-
Syntax
var json-object-name = {"string" : number_value, .......}
Example
Example showing Number Datatype, value should not be quoted:
var obj = {"marks": 97}
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:
Type Description
" double quotation
 backslash
/ forward slash
b backspace
f form feed
n new line
JSON
11
r carriage return
t horizontal tab
u four hexadecimal digits
Syntax
var json-object-name = { string : "string value", .......}
Example
Example showing String Datatype:
var obj = {"name": "Amit"}
Boolean
It includes true or false values.
Syntax
var json-object-name = { string : true/false, .......}
Example
var obj = {"name": "Amit", "marks": 97, "distinction": true}
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.
JSON
12
Syntax
[ value, .......]
Example
Example showing array containing multiple objects:
{
"books": [
{ "language":"Java" , "edition":"second" },
{ "language":"C++" , "lastName":"fifth" },
{ "language":"C" , "lastName":"third" }
]
}
JSON
13
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com

More Related Content

PDF
Node-IL Meetup 12/2
PDF
Json tutorial
PPTX
PPTX
Json training
PPTX
Introduction to JavaScript Object Notation(JSON)
PDF
Json
PPTX
JSON.pptx
Node-IL Meetup 12/2
Json tutorial
Json training
Introduction to JavaScript Object Notation(JSON)
Json
JSON.pptx

Similar to Json tutorial (20)

PDF
Introduction to JavaScript Object Notation, Lindsay Bassett, 2015
PPTX
JSON - (English)
PPTX
Json
PPT
json.ppt download for free for college project
PPTX
LU 1.3. JSON & XML.pptx about how they work and introduction
PPT
Json – java script object notation
PPT
Json - ideal for data interchange
PDF
Json the-x-in-ajax1588
PDF
JSON PRETTIFY.pdf
PPTX
All about XML, JSON and related topics..
PDF
Json at work overview and ecosystem-v2.0
PDF
Introduction to JSON
PPT
Json – java script object notation
PPTX
J s o n
PPTX
Intro to JSON
PPT
Java Script Object Notation (JSON)
Introduction to JavaScript Object Notation, Lindsay Bassett, 2015
JSON - (English)
Json
json.ppt download for free for college project
LU 1.3. JSON & XML.pptx about how they work and introduction
Json – java script object notation
Json - ideal for data interchange
Json the-x-in-ajax1588
JSON PRETTIFY.pdf
All about XML, JSON and related topics..
Json at work overview and ecosystem-v2.0
Introduction to JSON
Json – java script object notation
J s o n
Intro to JSON
Java Script Object Notation (JSON)
Ad

More from Ashoka Vanjare (20)

PDF
Tika tutorial
PDF
Sqlite perl
PDF
Sqoop tutorial
PDF
Xpath tutorial
PDF
Xml tutorial
PDF
Xsd tutorial
PDF
Xslt tutorial
PDF
Xquery xpath
PDF
Postgresql tutorial
PDF
Postgresql quick guide
PDF
Perl tutorial final
PDF
PDF
Php7 tutorial
PDF
Mongodb tutorial
PDF
Maven tutorial
PDF
Mahout tutorial
PDF
Learn embedded systems tutorial
PDF
Learn data structures algorithms tutorial
PDF
Learn c standard library
PDF
Learn c programming
Tika tutorial
Sqlite perl
Sqoop tutorial
Xpath tutorial
Xml tutorial
Xsd tutorial
Xslt tutorial
Xquery xpath
Postgresql tutorial
Postgresql quick guide
Perl tutorial final
Php7 tutorial
Mongodb tutorial
Maven tutorial
Mahout tutorial
Learn embedded systems tutorial
Learn data structures algorithms tutorial
Learn c standard library
Learn c programming
Ad

Recently uploaded (20)

DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
PPTX
web development for engineering and engineering
PPTX
Lecture Notes Electrical Wiring System Components
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PPTX
Construction Project Organization Group 2.pptx
PPTX
Internet of Things (IOT) - A guide to understanding
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PPTX
CH1 Production IntroductoryConcepts.pptx
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
DOCX
573137875-Attendance-Management-System-original
PPTX
Geodesy 1.pptx...............................................
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PDF
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
IOT PPTs Week 10 Lecture Material.pptx of NPTEL Smart Cities contd
web development for engineering and engineering
Lecture Notes Electrical Wiring System Components
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Construction Project Organization Group 2.pptx
Internet of Things (IOT) - A guide to understanding
R24 SURVEYING LAB MANUAL for civil enggi
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
UNIT-1 - COAL BASED THERMAL POWER PLANTS
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
CH1 Production IntroductoryConcepts.pptx
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
573137875-Attendance-Management-System-original
Geodesy 1.pptx...............................................
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
The CXO Playbook 2025 – Future-Ready Strategies for C-Suite Leaders Cerebrai...

Json tutorial

  • 2. JSON i AbouttheTutorial JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. The JSON format was originally specified by Douglas Crockford, and is described in RFC 4627. The official Internet media type for JSON is application/json. The JSON filename extension is .json. This tutorial will help you understand JSON and its use within various programming languages such as PHP, PERL, Python, Ruby, Java, etc. Audience This tutorial has been designed to help beginners understand the basic functionality of JavaScript Object Notation (JSON) to develop the data interchange format. After completing this tutorial, you will have a good understanding of JSON and how to use it with JavaScript, Ajax, Perl, etc. Prerequisites Before proceeding with this tutorial, you should have a basic understanding of the web application’s work over HTTP and we assume that you have a basic knowledge of JavaScript. Copyright&Disclaimer  Copyright 2017 by Tutorials Point (I) Pvt. Ltd. All the content and graphics published in this e-book are the property of Tutorials Point (I) Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish any contents or a part of contents of this e-book in any manner without written consent of the publisher. We strive to update the contents of our website and tutorials as timely and as precisely as possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt. Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website or its contents including this tutorial. If you discover any errors on our website or in this tutorial, please notify us at contact@tutorialspoint.com
  • 3. JSON ii TableofContents About the Tutorial....................................................................................................................................i Audience..................................................................................................................................................i Prerequisites............................................................................................................................................i Copyright & Disclaimer.............................................................................................................................i Table of Contents....................................................................................................................................ii 1. JSON ─ OVERVIEW...............................................................................................................1 Uses of JSON ...........................................................................................................................................1 Characteristics of JSON............................................................................................................................1 Simple Example in JSON..........................................................................................................................1 2. JSON ─ SYNTAX....................................................................................................................4 3. JSON ─ DATATYPES..............................................................................................................5 Number...................................................................................................................................................5 String ......................................................................................................................................................6 Boolean...................................................................................................................................................7 Array.......................................................................................................................................................7 Object .....................................................................................................................................................8 Whitespace .............................................................................................................................................8 null..........................................................................................................................................................9 JSON Value..............................................................................................................................................9 4. JSON ─ OBJECTS.................................................................................................................10 Creating Simple Objects ........................................................................................................................10 Creating Array Objects ..........................................................................................................................11
  • 4. JSON iii 5. JSON ─ SCHEMA ................................................................................................................13 JSON Schema Validation Libraries .........................................................................................................13 JSON Schema Example ..........................................................................................................................14 6. JSON ─ COMPARISON WITH XML.......................................................................................17 7. JSON ─ JSON WITH PHP.....................................................................................................18 Environment .........................................................................................................................................18 JSON Functions......................................................................................................................................18 Encoding JSON in PHP (json_encode) ....................................................................................................18 Decoding JSON in PHP (json_decode)....................................................................................................19 8. JSON ─ JSON WITH PERL....................................................................................................21 Environment .........................................................................................................................................21 JSON Functions......................................................................................................................................21 Encoding JSON in Perl (encode_json) ....................................................................................................22 Decoding JSON in Perl (decode_json) ....................................................................................................23 9. JSON ─ JSON WITH PYTHON ..............................................................................................25 Environment .........................................................................................................................................25 JSON Functions......................................................................................................................................25 Encoding JSON in Python (encode)........................................................................................................25 Decoding JSON in Python (decode) .......................................................................................................26 10. JSON ─ JSON WITH RUBY...................................................................................................27 Environment .........................................................................................................................................27 Parsing JSON using Ruby .......................................................................................................................27
  • 5. JSON iv 11. JSON ─ JSON WITH JAVA....................................................................................................29 Environment .........................................................................................................................................29 Mapping between JSON and Java entities.............................................................................................29 Encoding JSON in Java...........................................................................................................................30 Decoding JSON in Java...........................................................................................................................31 12. JSON ─ JSON WITH AJAX....................................................................................................33
  • 6. JSON 5 JSON or JavaScript Object Notation is a lightweight text-based open standard designed for human-readable data interchange. Conventions used by JSON are known to programmers, which include C, C++, Java, Python, Perl, etc.  JSON stands for JavaScript Object Notation.  The format was specified by Douglas Crockford.  It was designed for human-readable data interchange.  It has been extended from the JavaScript scripting language.  The filename extension is .json.  JSON Internet Media type is application/json.  The Uniform Type Identifier is public.json. UsesofJSON  It is used while writing JavaScript based applications that includes browser extensions and websites.  JSON format is used for serializing and transmitting structured data over network connection.  It is primarily used to transmit data between a server and web applications.  Web services and APIs use JSON format to provide public data.  It can be used with modern programming languages. CharacteristicsofJSON  JSON is easy to read and write.  It is a lightweight text-based interchange format.  JSON is language independent. SimpleExampleinJSON The following example shows how to use JSON to store information related to books based on their topic and edition. 1. JSON ─ OVERVIEW
  • 7. JSON 6 { "book": [ { "id":"01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, { "id":"07", "language": "C++", "edition": "second" "author": "E.Balagurusamy" }] } After understanding the above program, we will try another example. Let's save the below code as json.htm: <html> <head> <title>JSON example</title> <script language="javascript" > var object1 = { "language" : "Java", "author" : "herbert schildt" }; document.write("<h1>JSON with JavaScript example</h1>"); document.write("<br>"); document.write("<h3>Language = " + object1.language+"</h3>"); document.write("<h3>Author = " + object1.author+"</h3>"); var object2 = { "language" : "C++", "author" : "E-Balagurusamy" }; document.write("<br>"); document.write("<h3>Language = " + object2.language+"</h3>");
  • 8. JSON 7 document.write("<h3>Author = " + object2.author+"</h3>"); document.write("<hr />"); document.write(object2.language + " programming language can be studied " + "from book written by " + object2.author); document.write("<hr />"); </script> </head> <body> </body> </html> Now let's try to open json.htm using IE or any other javascript enabled browser that produces the following result: You can refer to JSON Objects chapter for more information on JSON objects.
  • 9. JSON 8 Let's have a quick look at the basic syntax of JSON. 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). Below is a simple example: { "book": [ { "id":"01", "language": "Java", "edition": "third", "author": "Herbert Schildt" }, { "id":"07", "language": "C++", "edition": "second" "author": "E.Balagurusamy" }] } JSON supports the following two data structures:  Collection of name/value pairs: This Data Structure is supported by different programming languages.  Ordered list of values: It includes array, list, vector or sequence etc. 2. JSON ─ SYNTAX
  • 10. JSON 9 JSON format supports the following data types: Type Description Number double- precision floating-point format in JavaScript String double-quoted Unicode with backslash escaping Boolean true or false Array an ordered sequence of values Value it can be a string, a number, true or false, null etc Object an unordered collection of key:value pairs Whitespace can be used between any pair of tokens null empty Number  It is 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: Type Description 3. JSON ─ DATATYPES
  • 11. JSON 10 Integer Digits 1-9, 0 and positive or negative Fraction Fractions like .3, .9 Exponent Exponent like e, e+, e-,E, E+, E- Syntax var json-object-name = {"string" : number_value, .......} Example Example showing Number Datatype, value should not be quoted: var obj = {"marks": 97} 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: Type Description " double quotation backslash / forward slash b backspace f form feed n new line
  • 12. JSON 11 r carriage return t horizontal tab u four hexadecimal digits Syntax var json-object-name = { string : "string value", .......} Example Example showing String Datatype: var obj = {"name": "Amit"} Boolean It includes true or false values. Syntax var json-object-name = { string : true/false, .......} Example var obj = {"name": "Amit", "marks": 97, "distinction": true} 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.
  • 13. JSON 12 Syntax [ value, .......] Example Example showing array containing multiple objects: { "books": [ { "language":"Java" , "edition":"second" }, { "language":"C++" , "lastName":"fifth" }, { "language":"C" , "lastName":"third" } ] }
  • 14. JSON 13 End of ebook preview If you liked what you saw… Buy it from our store @ https://store.tutorialspoint.com