0% found this document useful (0 votes)
39 views

Untitled

JSON is a lightweight data format that uses human-readable text to transmit data objects consisting of attribute-value pairs. It can be used to validate and parse JSON files in Python using the json module. This allows Python programs to easily write JSON to files, read JSON from files, and access properties within JSON objects.

Uploaded by

shrishtijain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views

Untitled

JSON is a lightweight data format that uses human-readable text to transmit data objects consisting of attribute-value pairs. It can be used to validate and parse JSON files in Python using the json module. This allows Python programs to easily write JSON to files, read JSON from files, and access properties within JSON objects.

Uploaded by

shrishtijain
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

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
{ "address": { "children": {
"type": "object", "type": "object", "type": "array",
"properties": { "properties": {
"items": [
"name": { "street": {
{
"type": "string"
"type": "string"
}, "type": "string"
},
"city": { }
"age": {
"type": "string" ]
"type": "integer"
} }
}, }
}
},
}
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

You might also like