[go: up one dir, main page]

0% found this document useful (0 votes)
7 views36 pages

Introduction to JSON Lecture22

The document provides an introduction to JSON (JavaScript Object Notation), a lightweight and language-independent data format used for storing and transmitting structured data. It explains JSON's syntax, data types, and its comparison with other formats like XML, highlighting its readability and efficiency. Additionally, it discusses JSON Schema for data validation and the significance of using double quotes for strings in JSON.

Uploaded by

artimist0611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views36 pages

Introduction to JSON Lecture22

The document provides an introduction to JSON (JavaScript Object Notation), a lightweight and language-independent data format used for storing and transmitting structured data. It explains JSON's syntax, data types, and its comparison with other formats like XML, highlighting its readability and efficiency. Additionally, it discusses JSON Schema for data validation and the significance of using double quotes for strings in JSON.

Uploaded by

artimist0611
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 36

Introduction to

JSON
Opening question…..

"How do websites and apps like Instagram or


Amazon know what content to show you—and
how do they send that information back and
forth between your device and their servers?"
Reflections:
Sets up the need for a universal, readable, and
structured data format—which leads directly
into JSON.

To solve this problem, we need a language-


neutral way to structure and send data reliably.
One of the most widely used formats for that is
JSON.

Today, we’re going to explore what JSON is, why


it’s used everywhere from APIs to configuration
files, and how you can use it effectively.
What is JSON?
■JSON: JavaScript Object Notation.
■JSON is a simple, text-based way to store
and transmit structured data. By using a
simple syntax, the user can easily store
anything from a single number through to
strings, arrays, and objects using nothing but
a string of plain text. You can also nest arrays
and objects, allowing you to create complex
data structures.
What is JSON?
■JSON is a syntax for storing and exchanging
data.
■JSON is text (no binary data).
■JSON is a lightweight data-interchange
format
■JSON is "self-describing" and easy to
understand
■JSON is language independent.
JSON Schema
JSON Schema is a declarative language that provides
a standardized way to describe and validate JSON
data. It describes the content, structure, data types,
and expected constraints within a JSON document,
which helps ensure the consistency and integrity of
JSON data in different applications.
A schema example can be a young child's
schema for a dog, where they understand that dogs
are furry, have four legs, and a tail. Another example
is a person's schema for their first day at work, where
they have expectations based on conversations with
current employees about a typical workday. In the
context of databases, a schema defines the structure
of data, including tables, columns, data types, and
relationships. For example, an e-commerce platform
might have a schema for customers, products, orders,
JSON vs Other Data Formats
Property JSON XML YAML Protocol Buffers
(Protobuf)

Readability High Medium High Low (binary)


Verbosity Low High Medium Very low

Data Types Strong support All values are Supports various Strong, typed
strings types schema
Parsing Speed Fast Slower Slower Very fast

Schema Support Optional (JSON Strong (DTD, XSD) Weak Required


Schema)
Extensibility Medium High Medium High
Binary Support No No No Yes

Use Case APIs, config files Document Config files, Performance-


Suitability structure, legacy DevOps tools critical systems
JSON is NOT
■Overly Complex
■A 'document' format
■A markup language
■A programming
language
JSON Syntax
JSON Syntax Rules
■Data is in name/value pairs
■Data is separated by commas
■Curly braces hold objects i.e.
{}
■Square brackets hold arrays
i.e. [ ]
JSON Object Syntax
■Unordered sets of name/value pairs
■Starts with { (left brace)
■Ends with } (right brace)
■Each name is followed by : (colon) and then
its value
■Name/value pairs are separated by ,
(comma)
JSON Data - A Name and a
Value
■JSON data is written as name/value pairs.
■A name/value pair consists of a field name
(in double quotes), followed by a colon,
followed by a value:
■Example
{ "name" : "John" }
Significance of Using Double Quotes
for Strings in JSON
• Double quotes are mandatory for strings and
keys in JSON.
• They ensure valid, predictable, and
interoperable parsing.
• Violating this rule leads to errors or
incompatibility, especially when consuming
JSON with strict parsers or across different
systems.
• Double quotes prevent conflicts or
misinterpretation when parsing data.
• JSON parsers use double quotes to
deterministically identify string boundaries.
JSON Arrays
■An ordered collection of values
■Starts with [ (left bracket)
■Ends with ] (right bracket)
■Name/value pairs are separated by ,
(comma)
JSON Array Example

{
"name" : "John",
"cars" : [ "Ford", "BMW",
"Fiat" ]
}
JSON Data Types
JSON Values
■In JSON, values must be one of the following data
types:

a string

a number

an object (JSON

object) an array

a

boolean
null
■JSON values cannot be one of the following data
types:

a

function

a date
undefine
d
JSON Strings
■Strings in JSON must be written in double
quotes.
■Example
{ "name" : "John" }
JSON Numbers
■Numbers in JSON must be an integer or a
floating point.
■Example
{ "age" : 30 }
JSON Objects
■Values in JSON can be objects.
■Example
{
"employee" : { "name" : "John", "age" : 30, "city" :
"New York" }
}
JSON Arrays
■Values in JSON can be arrays.
■Example
{
"employees" : [ "John", "Anna",
"Peter" ]
}
JSON Booleans
■Values in JSON can be true or
false.
■Example
{ "sale" : true }
JSON null
■Values in JSON can be
null.
■Example
{ "middlename" : null }
Nested JSON Objects
■Values in a JSON object can be another JSON
object.

Example
myObj =
{
"name":"John
", "age":30,
"cars" : {
"car1
":
"Ford
",
"car2
":
Arrays in JSON Objects
■Arrays can be values of an object
property:
■Example
myObj = {

"name" :
"John", "age" :
30,
"cars" : [

"Ford",
Nested Arrays in JSON Objects
■Values in an array can also be another array, or even another
JSON object:
■Example
myObj = {
"name" :
"John", "age" :
30,
"cars" : [
{ "nam
e" :
"Ford",
"model
s":
[ "Fiest
a",
"Focus"
,
"Musta
ng" ] },
{ "nam
e" :
"BMW",
"model
s" :
[ "320",
JSON Data Example
JSON vs XML
■Both JSON and XML can be used as a data
format when sending/receiving data
between servers.
■The following JSON and XML examples both
defines an employees object, with an array of
3 employees:
JSON vs XML
{"employees": [
{ "firstName":"John", "lastName":"Doe" },
{ "firstName":"Anna", "lastName":"Smith" },
{ "firstName":"Peter", "lastName":"Jones" }
]
}
<employees>
<employee>
<firstName>John</firstName>
<lastName>Doe</lastName>
</employee>
<employee>
<firstName>Anna</firstName>
<lastName>Smith</lastName>
</employee>
<employee>
<firstName>Peter</firstName>
<lastName>Jones</lastName>
</employee>
JSON vs XML
■JSON is like XML
because:

Both JSON and XML are "self
describing" (human readable)

Both JSON and XML are hierarchical
(values within values)

Both JSON and XML can be parsed and
used by
lots of programming languages
JSON vs XML
■JSON is unlike XML
because:

JSON doesn't use end

tag JSON is shorter

JSON is quicker to read and

write JSON can use arrays
JSON vs XML
■Since XML is widely used as data interchange
format, we will try to draw a comparison
between them. The purpose of the
comparison is not to determine which is
better but rather we will try to understand
which one is suitable for storing specific kind
of data.
JSON vs XML
■XML is more expressive than JSON. XML
sometimes also suffers from using tags
repeatedly, where as JSON is much more
concise.
■XML is more complex than JSON.
■JSON lacks namespaces
■There are several specifications to define
schema(metadata) for XML, for example
DTD and XSD. JSON schema is available but
is not as widely used as XML schemas.
JSON vs XML
■XML and JSON can be used with most of the programming
languages. The problem comes when matching XML tags
used by one system may be different in another system,
hence, XML data will require transformation.
i.e. <FirstName> vs <first_name>.
In the case of JSON, since objects and arrays are basic
data structures used, it is easy to work with them in
programs.
■For selecting specific parts of an XML document, there is
standard specification called XPath. This is widely used. In
JSON, we have JSONPath to do the same, but it is not
widely used.
JSON vs XML – Detailed Comparison
Feature JSON XML
Format Type Data-interchange format Markup language
Readability More concise and readable More verbose
Uses nested elements and
Data Structure Uses objects and arrays
attributes
Strong (via attributes and
Support for Metadata Limited
DTD/XSD)

Supports native types (string, All values are strings unless


Data Types
number, boolean, array, null) validated

Slower (more complex


Parsing Speed Faster (lighter syntax)
structure)
Validation JSON Schema (less mature) DTD/XSD (well-established)
Namespace Support No Yes
Extensibility Limited High
Legacy systems, document
Usage Contexts Web APIs, configuration files
storage
Questions & Answers

You might also like