SlideShare a Scribd company logo
25-Mar-24
JSON
Outline
 What is JSON?
 Syntax
 Example
 Schema
 Validating JSON file
 Using JSON with Python
 Writing a JSON file
 Reading a JSON
 Accessing JSON Properties in Python
What is JSON?
 “JSON” stands for “JavaScript Object Notation”
 Lightweight data-interchange format
 Despite the name, JSON is a (mostly) language-
independent way of specifying objects as name-value
pairs
 Structured representation of data object
 Can be parsed with most modern languages
 JSON Schema can be used to validated a JSON
file
JSON Syntax Rules
 JSON is almost identical to python dictionary except for
 In JSON, true and false are not capitalized
 In JSON, null is used instead of None
 Uses key/value pairs: {“name”: “John”}
 Uses double quotes around KEY and VALUE
 Must use the specified types
 File type is “.json”
 A value can be: A string, a number, true, false, null, an
object, or an array
 Strings are enclosed in double quotes, and can contain
the usual assortment of escaped characters
JSON Example
{
"name": "John Smith",
"age": 35,
"address": {
"street": "5 main St.",
"city": "Austin"
},
"children": ["Mary", "Abel"]
}
JSON Schema
 A JSON Schema allows you to specify what type of
data can go into your JSON files.
 It allows you to restrict the type of data entered.
JSON Schema
{
"type": "object",
"properties": {
"name": {
"type": "string"
},
"age": {
"type": "integer"
},
"address": {
"type": "object",
"properties": {
"street": {
"type": "string"
},
"city": {
"type": "string"
}
}
"children": {
"type": "array",
"items": {
"type": "string
}
}
}
}
Validating JSON file
 The following website can be used to validate a JSON
file against a schema
https://www.jsonschemavalidator.net/
 Paste both the schema and the corresponding JSON file
Using JSON with Python
 To work with JSON (string, or file containing JSON
object), you can use Python's json module.
import json
Loading JSON data from a file
 Example:
def load_json(filename):
with open(filename) as file:
jsn = json.load(file)
#file.close()
return jsn
person = load_json('person.json')
 This command parse the above person.json using
json.load() method from the json module. The result is a
Python dictionary.
Writing JSON object to a file
 Example:
person = { "name": "John Smith", "age": 35,
"address": {"street": "5 main St.", "city":
"Austin"}, "children": ["Mary", "Abel"]}
with open('person_to_json.json', 'w') as fp:
json.dump(person, fp, indent=4)
 Using json.dump(), we can convert Python Objects to
JSON file.
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property "name"
 Print(person["name"])
 John Smith
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property “age"
 person["age"]
 35
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property “street"
 print(person["address"]["street"])
 5 main St.
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property “street"
 print(person["address"]["city"])
 Austin
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property “street"
 print(person["children"][0])
 Mary
Accessing JSON Properties in Python
 Example:
Assume that you already loaded your person.json as
follows.
person = load_json('person.json')
To access the property “street"
 print(person["children"][1])
 Abel
Python – JSON Objects
Credit:
 https://www.youtube.com/watch?v=wI1CWzNtE-M
 https://www.programiz.com/python-programming/json

More Related Content

Similar to JSON(JavaScript Object Notation) Presentation transcript (20)

PPTX
Json
Uma mohan
 
PDF
PyLecture3 -json-
Yoshiki Satotani
 
PDF
Json tutorial
Ashoka Vanjare
 
PPTX
JSON.pptx
TilakaRt
 
PDF
Dealing with JSON files in python with illustrations
Kiran Kumaraswamy
 
PDF
JSON PRETTIFY.pdf
json beautifier
 
PPTX
Files.pptx
Govardhan Bhavani
 
PDF
Data Serialization in Python JSON vs. Pickle
Inexture Solutions
 
PPTX
JSON - (English)
Senior Dev
 
PPTX
1 Json Intro and datatype PRESENTATION.pptx
angelinjeba6
 
PDF
What is Python JSON | Edureka
Edureka!
 
PDF
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
Elżbieta Bednarek
 
PPTX
Json
primeteacher32
 
PDF
JSON beautifier (1).pdf
json beautifier
 
PPT
Json – java script object notation
Pankaj Srivastava
 
PPTX
Intro to JSON
Mark Daniel Dacer
 
PDF
Json
soumya
 
PDF
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 
Json
Uma mohan
 
PyLecture3 -json-
Yoshiki Satotani
 
Json tutorial
Ashoka Vanjare
 
JSON.pptx
TilakaRt
 
Dealing with JSON files in python with illustrations
Kiran Kumaraswamy
 
JSON PRETTIFY.pdf
json beautifier
 
Files.pptx
Govardhan Bhavani
 
Data Serialization in Python JSON vs. Pickle
Inexture Solutions
 
JSON - (English)
Senior Dev
 
1 Json Intro and datatype PRESENTATION.pptx
angelinjeba6
 
What is Python JSON | Edureka
Edureka!
 
FIFA fails, Guy Kawasaki and real estate in SF - find out about all three by ...
Elżbieta Bednarek
 
JSON beautifier (1).pdf
json beautifier
 
Json – java script object notation
Pankaj Srivastava
 
Intro to JSON
Mark Daniel Dacer
 
Json
soumya
 
Json at work overview and ecosystem-v2.0
Boulder Java User's Group
 

Recently uploaded (20)

PDF
Smarter Private Job Search Starts with Formwalaa
Reeshna Prajeesh
 
PPTX
原版英国牛津大学毕业证(Oxon毕业证书)如何办理
Taqyea
 
PDF
Find the Right Private Job Faster with Formwalaa
Reeshna Prajeesh
 
PDF
Sarkari Job Alerts in Marathi & English – Majhi Naukri
Reeshna Prajeesh
 
PPTX
Learn AI in Software Testing - Venkatesh (Rahul Shetty)
Venkatesh (Rahul Shetty)
 
PDF
Bilal Ibrar | Digital Marketing Expert | Resume | CV
Bilal Ibrar
 
PPTX
Web Developer Jobs in Jaipur Your Gateway to a Thriving Tech Career in Rajast...
vinay salarite
 
PDF
Buy Twitter Accounts_ Boost Your Brand and Reach in 2025 - Copy - Copy.pdf
Buy Old Twitter Accounts-100% Secure Aged With Followers
 
PDF
All-in-One Government Job Portal – Discover MajhiNaukri
Reeshna Prajeesh
 
PPTX
Wow a powerpoint I'm really helping humanity
yangjessica629
 
PDF
Get Daily Sarkari Job Updates – Majhi Naukri
Reeshna Prajeesh
 
PPTX
文凭复刻澳洲电子毕业证阳光海岸大学成绩单USC录取通知书
Taqyea
 
PDF
ILLUMINATI CALL KAMPALA IN UGANDA+256776963507/0741506136
Illuminati Agent in Kampala Uganda+256776963507/0741506136
 
PPTX
Data Analyst Jobs in Jaipur Top Hiring Trends in 2025.pptx
vinay salarite
 
PPTX
Leadership Principles Presentations.pptx
ChrisBus1
 
PDF
hr generalist certification.pdf.........
a25075044
 
PPTX
WAT - 2 - Copy.pptxAT - 1.pptx review this to get sutaible help
pranksta001
 
PDF
NotificationForTheTeachingPositionsAdvt012025.pdf
sunitsaathi
 
PPT
SQL.pptkarim pfe rabatkarim pfe rabatkarim pfe rabat
Keeyvikyv
 
PDF
From-Idea-to-Business-Plan-A-Practical-Guide.pdf
eman youssif
 
Smarter Private Job Search Starts with Formwalaa
Reeshna Prajeesh
 
原版英国牛津大学毕业证(Oxon毕业证书)如何办理
Taqyea
 
Find the Right Private Job Faster with Formwalaa
Reeshna Prajeesh
 
Sarkari Job Alerts in Marathi & English – Majhi Naukri
Reeshna Prajeesh
 
Learn AI in Software Testing - Venkatesh (Rahul Shetty)
Venkatesh (Rahul Shetty)
 
Bilal Ibrar | Digital Marketing Expert | Resume | CV
Bilal Ibrar
 
Web Developer Jobs in Jaipur Your Gateway to a Thriving Tech Career in Rajast...
vinay salarite
 
Buy Twitter Accounts_ Boost Your Brand and Reach in 2025 - Copy - Copy.pdf
Buy Old Twitter Accounts-100% Secure Aged With Followers
 
All-in-One Government Job Portal – Discover MajhiNaukri
Reeshna Prajeesh
 
Wow a powerpoint I'm really helping humanity
yangjessica629
 
Get Daily Sarkari Job Updates – Majhi Naukri
Reeshna Prajeesh
 
文凭复刻澳洲电子毕业证阳光海岸大学成绩单USC录取通知书
Taqyea
 
ILLUMINATI CALL KAMPALA IN UGANDA+256776963507/0741506136
Illuminati Agent in Kampala Uganda+256776963507/0741506136
 
Data Analyst Jobs in Jaipur Top Hiring Trends in 2025.pptx
vinay salarite
 
Leadership Principles Presentations.pptx
ChrisBus1
 
hr generalist certification.pdf.........
a25075044
 
WAT - 2 - Copy.pptxAT - 1.pptx review this to get sutaible help
pranksta001
 
NotificationForTheTeachingPositionsAdvt012025.pdf
sunitsaathi
 
SQL.pptkarim pfe rabatkarim pfe rabatkarim pfe rabat
Keeyvikyv
 
From-Idea-to-Business-Plan-A-Practical-Guide.pdf
eman youssif
 
Ad

JSON(JavaScript Object Notation) Presentation transcript

  • 2. Outline  What is JSON?  Syntax  Example  Schema  Validating JSON file  Using JSON with Python  Writing a JSON file  Reading a JSON  Accessing JSON Properties in Python
  • 3. What is JSON?  “JSON” stands for “JavaScript Object Notation”  Lightweight data-interchange format  Despite the name, JSON is a (mostly) language- independent way of specifying objects as name-value pairs  Structured representation of data object  Can be parsed with most modern languages  JSON Schema can be used to validated a JSON file
  • 4. JSON Syntax Rules  JSON is almost identical to python dictionary except for  In JSON, true and false are not capitalized  In JSON, null is used instead of None  Uses key/value pairs: {“name”: “John”}  Uses double quotes around KEY and VALUE  Must use the specified types  File type is “.json”  A value can be: A string, a number, true, false, null, an object, or an array  Strings are enclosed in double quotes, and can contain the usual assortment of escaped characters
  • 5. JSON Example { "name": "John Smith", "age": 35, "address": { "street": "5 main St.", "city": "Austin" }, "children": ["Mary", "Abel"] }
  • 6. JSON Schema  A JSON Schema allows you to specify what type of data can go into your JSON files.  It allows you to restrict the type of data entered.
  • 7. JSON Schema { "type": "object", "properties": { "name": { "type": "string" }, "age": { "type": "integer" }, "address": { "type": "object", "properties": { "street": { "type": "string" }, "city": { "type": "string" } } "children": { "type": "array", "items": { "type": "string } } } }
  • 8. Validating JSON file  The following website can be used to validate a JSON file against a schema https://www.jsonschemavalidator.net/  Paste both the schema and the corresponding JSON file
  • 9. Using JSON with Python  To work with JSON (string, or file containing JSON object), you can use Python's json module. import json
  • 10. Loading JSON data from a file  Example: def load_json(filename): with open(filename) as file: jsn = json.load(file) #file.close() return jsn person = load_json('person.json')  This command parse the above person.json using json.load() method from the json module. The result is a Python dictionary.
  • 11. Writing JSON object to a file  Example: person = { "name": "John Smith", "age": 35, "address": {"street": "5 main St.", "city": "Austin"}, "children": ["Mary", "Abel"]} with open('person_to_json.json', 'w') as fp: json.dump(person, fp, indent=4)  Using json.dump(), we can convert Python Objects to JSON file.
  • 12. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property "name"  Print(person["name"])  John Smith
  • 13. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property “age"  person["age"]  35
  • 14. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property “street"  print(person["address"]["street"])  5 main St.
  • 15. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property “street"  print(person["address"]["city"])  Austin
  • 16. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property “street"  print(person["children"][0])  Mary
  • 17. Accessing JSON Properties in Python  Example: Assume that you already loaded your person.json as follows. person = load_json('person.json') To access the property “street"  print(person["children"][1])  Abel
  • 18. Python – JSON Objects