JSONMaster is a Python package designed to provide a collection of utilities for working with JSON data. Whatever you are doing with jsons, JSONMaster,aims to simplify your workflow and enhance your productivity.
- Easy Parsing: Effortlessly parse JSON strings into Python objects.
- Transformation: Transform JSON structures with intuitive functions to namespaces or dataclasses.
- File I/O: Simplify reading from and writing to JSON files.
You can install JSONMaster via pip:
pip install jsonmasterRead a json from a file path
from jsonmaster import open_json, JsonFile
# easy option
with open_json("jsonfile.json") as jf:
print(jf.dict())
# configurable option
with JsonFile(file_path="jsonfile.json", sort_keys=True, immediate_flush=True, prettify=True) as jf:
print(jf.dict())All regular json package capabilities are imported inside jsonmaster
import jsonmaster
data: str = '{"name": "Alice", "age": 30}'
d: dict = jsonmaster.loads(data)
print(jsonmaster.dumps(d))
# ...Use your jsons as an actual python class
from jsonmaster import open_json, JsonNamespace
# jsonfile.json contains {"key": "value"}
with open_json("jsonfile.json") as jf:
namespace: JsonNamespace = jf.namespace()
# access the key
print(namespace.key)
# set will not affect the original fileRead and write JSON files easily:
from jsonmaster import open_json
# jsonfile.json contains {"key": "value"}
with open_json("jsonfile.json") as jf:
# Adding a new key to the json file
jf["new_key"] = "new_value"
# Accessing an existing key in the json file
print(jf["old_key"])
print(jf["new_key"])Load your json file into your pydantic BaseModel easily
from jsonmaster import open_json
from pydantic import BaseModel
class MyModel(BaseModel):
my_model_str_key: str
my_model_int_key: int
with open_json('jsonfile.json') as jf:
m: MyModel = jf.dataclass(MyModel)
print(m.my_model_str_key)We welcome contributions! Please read our Contributing Guidelines for details on how to get started.
This project is licensed under the MIT License. See the LICENSE file for more information.
Created by Ethan Lerner.