JSON¶
In [1]:
Copied!
# comment this out to use default `print` function
from rich import print
# comment this out to use default `print` function
from rich import print
Basic Usage¶
In [2]:
Copied!
# import the necessary module
from pyeio import json
# import the necessary module
from pyeio import json
Parsing serialized data¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.parse
In [3]:
Copied!
python_dictionary = json.parse(data='{"color":"green", "count": 3}')
python_dictionary
python_dictionary = json.parse(data='{"color":"green", "count": 3}')
python_dictionary
Out[3]:
{'color': 'green', 'count': 3}
Serializing python data¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.serialize
In [4]:
Copied!
json_string = json.serialize(python_dictionary, compact_serialization=False)
json_string
json_string = json.serialize(python_dictionary, compact_serialization=False)
json_string
Out[4]:
'{\n "color": "green", \n "count": 3\n}'
Opening a file¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.open
In [5]:
Copied!
# load the data into memory
python_dictionary = json.open(path="../../data/json/books.json")
# print the data
print(python_dictionary)
# load the data into memory
python_dictionary = json.open(path="../../data/json/books.json")
# print the data
print(python_dictionary)
{ 'books': [ { 'id': 1, 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960, 'genres': ['Fiction', 'Classic', 'Coming-of-age'], 'rating': 4.27, 'inStock': True }, { 'id': 2, 'title': '1984', 'author': 'George Orwell', 'year': 1949, 'genres': ['Fiction', 'Dystopian', 'Political'], 'rating': 4.18, 'inStock': False }, { 'id': 3, 'title': 'Pride and Prejudice', 'author': 'Jane Austen', 'year': 1813, 'genres': ['Fiction', 'Romance', 'Classic'], 'rating': 4.25, 'inStock': True } ], 'totalBooks': 3, 'lastUpdated': '2023-05-15T14:30:00Z' }
Saving data to a file¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.save
In [6]:
Copied!
# defin sample data
python_dictionary = {"message": "hello world", "data": [1, 2, 3, 4]}
# save to local temp file
json.save(data=python_dictionary, path="./temp/temp.json", overwrite=True)
# defin sample data
python_dictionary = {"message": "hello world", "data": [1, 2, 3, 4]}
# save to local temp file
json.save(data=python_dictionary, path="./temp/temp.json", overwrite=True)
Loading data from a URL¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.load
In [7]:
Copied!
# load the data from url
python_dictionary = json.load(
url="https://raw.githubusercontent.com/harttraveller/pyeio/refs/heads/main/data/json/books.json"
)
# print data
print(python_dictionary)
# load the data from url
python_dictionary = json.load(
url="https://raw.githubusercontent.com/harttraveller/pyeio/refs/heads/main/data/json/books.json"
)
# print data
print(python_dictionary)
{ 'books': [ { 'id': 1, 'title': 'To Kill a Mockingbird', 'author': 'Harper Lee', 'year': 1960, 'genres': ['Fiction', 'Classic', 'Coming-of-age'], 'rating': 4.27, 'inStock': True }, { 'id': 2, 'title': '1984', 'author': 'George Orwell', 'year': 1949, 'genres': ['Fiction', 'Dystopian', 'Political'], 'rating': 4.18, 'inStock': False }, { 'id': 3, 'title': 'Pride and Prejudice', 'author': 'Jane Austen', 'year': 1813, 'genres': ['Fiction', 'Romance', 'Classic'], 'rating': 4.25, 'inStock': True } ], 'totalBooks': 3, 'lastUpdated': '2023-05-15T14:30:00Z' }
Downloading data at a URL to a file¶
Reference: https://harttraveller.github.io/pyeio/module/pyeio.json/#pyeio.json.download
In [8]:
Copied!
# download data to local file
json.download(
url="https://raw.githubusercontent.com/harttraveller/pyeio/refs/heads/main/data/json/books.json",
path="./temp/books.json",
overwrite=True,
)
# download data to local file
json.download(
url="https://raw.githubusercontent.com/harttraveller/pyeio/refs/heads/main/data/json/books.json",
path="./temp/books.json",
overwrite=True,
)
Meta Module¶
TODO