(11)JSON:
JSON is a short form for 'Java Script Object Notation' which is a text-based, human-readable data interchange format used for representing simple data structure and objects in web-browser-based code.
It is heavily used in web applications. Python comes with a build-in JSON module which makes it easy to work with
for example:
1.Encoding(serialization) and decoding JSON data:
To convert the python dictionary to JSON format import the JSON module then make a variable and store the command 'Jason.dumps(name of python dictionary)'.
for example:
import json
person = {"name": "John", "age": 30, "city": "New York","hasChildren": False,
"titles": ["engineer" ,"programmer"]}
personJSON = json.dumps(person)
print(personJSON)
output = {"name": "John", "age": 30, "city": "New York", "hasChildren": false, "titles": ["engineer", "programmer"]}
To convert JSON to python
for example:
import json
person_json = """
{
"age": 30,
"city": "New York",
"hasChildren": false,
"name": "John",
"titles": [
"engineer",
"programmer"
]
}
"""
person = json.loads(person_json)
print(person)
output =
{'age': 30, 'city': 'New York', 'hasChildren': False, 'name': 'John', 'titles': ['engineer', 'programmer']}
To sort keys:
for example:
import json
person = {"name": "John", "age": 30, "city": "New York","hasChildren": False, "titles": ["engineer" ,"programmer"]}
personJSON = json.dumps(person, indent=4, sort_keys=True)
print(personJSON)
output =
{
"age": 30,
"city": "New York",
"hasChildren": false,
"name": "John",
"titles": [
"engineer",
"programmer"
]
}
To open the text in a file. This will make a json file and will have the text converted from python dict to JSON.
for example:
import json
person = {"name": "John", "age": 30, "city": "New York","hasChildren": False,
"titles": ["engineer" ,"programmer"]}
personJSON = json.dumps(person, indent=4, sort_keys=True)
print(personJSON)
with open('persons.json', 'w') as file:
json.dump(person, file)
output =
{
"age": 30,
"city": "New York",
"hasChildren": false,
"name": "John",
"titles": [
"engineer",
"programmer"
]
}
person.json file:
{"name": "John", "age": 30, "city": "New York", "hasChildren": false, "titles": ["engineer", "programmer"]}
To decode JSON file back to python format.
for example:
import json
person = {"name": "John", "age": 30, "city": "New York","hasChildren": False, "titles": ["engineer" ,"programmer"]}
personJSON = json.dumps(person, indent=4, sort_keys=True)
person = json.loads(personJSON)
print(person)
output =
{'age': 30, 'city': 'New York', 'hasChildren': False, 'name': 'John', 'titles': ['engineer', 'programmer']}
To decode variables in JSON.
for example:
import json
class User:
def __init__(self, name, age):
self.name = name
self.age = age
user = User('Max', 27)
def encode_user(o):
if isinstance(o, User):
return{'name': o.name, 'age':o.age, o.__class__.__name__: True}
else:
raise TypeError('object of type User is not JSON serializable')
useJSON = json.dumps(user, defualt=encode_user)
print(userJSON)
output =
{"name": "Max", "age": 27, "User": true}
Comments
Post a Comment
If you have any doubts please let me know