JSON File Helper
A Python class for easier JSON file handling. This utility provides a streamlined interface for reading, writing, appending, and validating JSON files, along with additional helper properties and methods.
Features
- Read JSON files with automatic file validation.
- Write JSON data (as dictionaries or lists of dictionaries) to files.
- Append data to existing JSON files, supporting both dictionaries and lists.
- Clear JSON files by resetting their content.
- Validate JSON schema using the
jsonschemalibrary. - Access properties like
keys,values,length,is_empty,size, andexists. - Pretty print JSON data for better readability.
Installation
Automatic installation via PyPi
pip install json_file_helper
Or manual installation
git clone https://github.com/your-repo/json-helper.git
cd json-helper
Install Dependencies
This module uses the jsonschema library. Install the requirements using pip or a package manager like Poetry:
pip install -r requirements.txt
Or with Poetry:
poetry install
Usage
Importing the Module
from json_file_helper import json_file
Creating an Instance
from json_file_helper import json_file
# Initialize with an optional file path
jf = json_file("example.json")
Methods and Properties
1. Reading a JSON File
data = jf.read()
print(data)
Or with the data property:
data = jf.data
print(data)
2. Writing Data
jf.write({"name": "John", "age": 30})
3. Appending Data
Append a dictionary to a JSON dictionary or list:
jf.append({"city": "New York"})
4. Clearing a File
jf.clear()
5. Validating Schema
Use a JSON schema to validate the structure of your file:
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"}
},
"required": ["name", "age"]
}
is_valid = jf.validate_schema(schema)
print(f"Is valid: {is_valid}")
6. Pretty Print
jf.pretty_print()
7. Accessing Properties
print(jf.keys) # List of keys in the JSON (if it's a dictionary)
print(jf.values) # List of values
print(jf.length) # Number of items (for dicts or lists)
print(jf.is_empty) # Check if the file is empty
print(jf.size) # File size in bytes
print(jf.exists) # Check if the file exists
Using as a Context Manager
The json_file class can also be used with with statements:
with json_file("example.json") as jf:
data = jf.read()
jf.append({"new_key": "new_value"})
Requirements
- Python 3.8+
jsonschemalibrary for schema validation
Example
Here’s a complete example:
from json_file_helper import json_file
# Initialize with a file path
jf = json_file("example.json")
# Write data to the file
jf.write({"name": "Alice", "age": 25})
# Read the file
print(jf.data)
# Append new data
jf.append({"city": "London"})
# Validate schema
schema = {
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
"city": {"type": "string"}
},
"required": ["name", "age"]
}
print(jf.validate_schema(schema))
# Pretty print the file's content
jf.pretty_print()
License
This project is licensed under the MIT License. See the LICENSE file for details.
Author
TheBiemGamer (biemgamer@pm.me)
Metadata
Release files for json_file_helper 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| json_file_helper-0.1.2.tar.gz | 4.0 kB | Without provenance | Details for json_file_helper-0.1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| json_file_helper-0.1.2-py3-none-any.whl | Python 3 | none | any | Details for json_file_helper-0.1.2-py3-none-any.whl |
Total release size: 8.9 kB
Release files / json_file_helper-0.1.2.tar.gz
| Download URL | json_file_helper-0.1.2.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
4c5e41fc4191a938f2f1384176a23319bca25c4a9e8539662d06856d40b33554
|
|
BLAKE2b-256 checksum How to use checksums |
bca9a8251721cae67276198bafb00ebd33153854d61c8a11365346d213b6e9d5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.13.1 Linux/6.12.8-2-cachyos
|
Release files / json_file_helper-0.1.2-py3-none-any.whl
| Download URL | json_file_helper-0.1.2-py3-none-any.whl |
|---|---|
| Size | 4.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ba49f7f6ba279c0edb707691ebc96306a777ba4d1cfb83f3128203030759bca5
|
|
BLAKE2b-256 checksum How to use checksums |
425953b2649416efd5c938e66a60cb0dcb3ed5d22deed5b70e7d2578f8c98dc2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.0.1 CPython/3.13.1 Linux/6.12.8-2-cachyos
|
Security for json_file_helper 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.