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)

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

Recently uploaded (20)

Pembuatan career yang baik dengan cara.pptx
Pembuatan career yang baik dengan cara.pptx
harun706371
 
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
Taqyea
 
Emotional_and_Social_Support_Presentation.pptx
Emotional_and_Social_Support_Presentation.pptx
mandiikkj
 
2025 English CV Sigve Hamilton Aspelund.docx
2025 English CV Sigve Hamilton Aspelund.docx
Sigve Hamilton Aspelund
 
How to Prepare for Palo Alto XDR Analyst Certification.pdf
How to Prepare for Palo Alto XDR Analyst Certification.pdf
Aakruthi Singh
 
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
deltaforcedxb7
 
challenges and role of government (1) (1).docx
challenges and role of government (1) (1).docx
vaibhavidd18
 
Strengths Based CV Template untuk latihan.docx
Strengths Based CV Template untuk latihan.docx
W Diana Ratri M.Psi, Psikolog
 
Gives a structured overview of the skills measured in the DP-700 exam
Gives a structured overview of the skills measured in the DP-700 exam
thehulk1299
 
Latest Avaya 71301T Exam Prep | Questions
Latest Avaya 71301T Exam Prep | Questions
sabrina pinto
 
ad seminar new for seminar 2 presentation
ad seminar new for seminar 2 presentation
dineshkumarengg
 
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
halderdhrubo6
 
Vibe Coding SoloLearn Certificate of Achievement Aniruddha Adak
Vibe Coding SoloLearn Certificate of Achievement Aniruddha Adak
ANIRUDDHA ADAK
 
最新版西班牙拉古纳大学毕业证(ULL毕业证书)原版定制
最新版西班牙拉古纳大学毕业证(ULL毕业证书)原版定制
Taqyea
 
Algebra fjuvrufguisdhvjbfjivjgrbvnbvjhu
Algebra fjuvrufguisdhvjbfjivjgrbvnbvjhu
mikylamongale
 
最新版美国埃默里大学毕业证(Emory毕业证书)原版定制
最新版美国埃默里大学毕业证(Emory毕业证书)原版定制
Taqyea
 
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
7300511143
 
Bbhuiiiugtuuuhhhhhhhhhhhhhhhhhhhhhhhhhhh
Bbhuiiiugtuuuhhhhhhhhhhhhhhhhhhhhhhhhhhh
HifzaNadeem5
 
Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025
Bruce Bennett
 
The best Strategies for Developing your Resume
The best Strategies for Developing your Resume
marcojaramillohenao0
 
Pembuatan career yang baik dengan cara.pptx
Pembuatan career yang baik dengan cara.pptx
harun706371
 
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
最新版美国威斯康星大学史蒂文分校毕业证(UWSP毕业证书)原版定制
Taqyea
 
Emotional_and_Social_Support_Presentation.pptx
Emotional_and_Social_Support_Presentation.pptx
mandiikkj
 
2025 English CV Sigve Hamilton Aspelund.docx
2025 English CV Sigve Hamilton Aspelund.docx
Sigve Hamilton Aspelund
 
How to Prepare for Palo Alto XDR Analyst Certification.pdf
How to Prepare for Palo Alto XDR Analyst Certification.pdf
Aakruthi Singh
 
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
"Housefull 5" (.2025.) +Fu𝗅𝗅Mov𝗂e! Down𝗅oad Fre𝖾 𝟩𝟤𝟢𝗉, 𝟦𝟪𝟢𝗉 𝖧𝖣 & 𝟣𝟢𝟪𝟢𝗉
deltaforcedxb7
 
challenges and role of government (1) (1).docx
challenges and role of government (1) (1).docx
vaibhavidd18
 
Gives a structured overview of the skills measured in the DP-700 exam
Gives a structured overview of the skills measured in the DP-700 exam
thehulk1299
 
Latest Avaya 71301T Exam Prep | Questions
Latest Avaya 71301T Exam Prep | Questions
sabrina pinto
 
ad seminar new for seminar 2 presentation
ad seminar new for seminar 2 presentation
dineshkumarengg
 
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
8queensproblemusingbacktracking-120903114053-phpapp01.pptx
halderdhrubo6
 
Vibe Coding SoloLearn Certificate of Achievement Aniruddha Adak
Vibe Coding SoloLearn Certificate of Achievement Aniruddha Adak
ANIRUDDHA ADAK
 
最新版西班牙拉古纳大学毕业证(ULL毕业证书)原版定制
最新版西班牙拉古纳大学毕业证(ULL毕业证书)原版定制
Taqyea
 
Algebra fjuvrufguisdhvjbfjivjgrbvnbvjhu
Algebra fjuvrufguisdhvjbfjivjgrbvnbvjhu
mikylamongale
 
最新版美国埃默里大学毕业证(Emory毕业证书)原版定制
最新版美国埃默里大学毕业证(Emory毕业证书)原版定制
Taqyea
 
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
Modern trends in Fruit Breedings , A new techniques of fruit Breedings
7300511143
 
Bbhuiiiugtuuuhhhhhhhhhhhhhhhhhhhhhhhhhhh
Bbhuiiiugtuuuhhhhhhhhhhhhhhhhhhhhhhhhhhh
HifzaNadeem5
 
Using Social Media in Job Search June 2025
Using Social Media in Job Search June 2025
Bruce Bennett
 
The best Strategies for Developing your Resume
The best Strategies for Developing your Resume
marcojaramillohenao0
 
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