This is a simple JSON Filehandler.
pip install noirpi-jsonhandler
Load a JSON file
from jsonhandler import FileIO
def load_json():
FileIO("./test.json")
or creates the file with a given value. If value is not given it uses an empty dict.
from jsonhandler import FileIO
def load_json():
FileIO("./test.json", {"test": True})
Save a JSON file
from jsonhandler import FileIO
def save_json():
file = FileIO("./test.json")
file.save()
Replaces the content of a JSON file
from jsonhandler import FileIO
def save_json():
file = FileIO("./test.json")
file.replace({"test": True})
Check if JSON file has valid Syntax
from jsonhandler import FileIO
def check_json():
file = FileIO("./test.json")
file.is_valid_json()
Check if JSON dictionary is has a valid syntax
from jsonhandler.FileIO import validate
def validate_json():
jsondict = {"test": "value"}
validate(jsondict)
Gets a value from JSON File
from jsonhandler import FileIO
def get_value():
file = FileIO("./test.json", {"test": {"key": True})
file["test"]
file["test", "key"]
Sets or adds a value inside a JSON File
from jsonhandler import FileIO
def get_value():
file = FileIO("./test.json")
file["test", "key"] = True
Delete a value inside a JSON File
from jsonhandler import FileIO
def delete_value():
jsondict = {"test": "value", "test1": {"test": "value"}}
file = FileIO("./test.json", jsondict)
file.delete(("test1", "test"))
or
from jsonhandler import FileIO
def delete_value():
jsondict = {"test": "value", "test1": {"test": "value"}}
file = FileIO("./test.json", jsondict)
del file["test1", "test"]