Skip to main content

Classes for easier file usage, currently only json is implemented.

Project description

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 jsonschema library.
  • Access properties like keys, values, length, is_empty, size, and exists.
  • 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+
  • jsonschema library 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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

json_file_helper-0.1.1.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

json_file_helper-0.1.1-py3-none-any.whl (4.9 kB view details)

Uploaded Python 3

File details

Details for the file json_file_helper-0.1.1.tar.gz.

File metadata

  • Download URL: json_file_helper-0.1.1.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/6.12.8-2-cachyos

File hashes

Hashes for json_file_helper-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1b897e380c07503dda7012fcb0864f91e19db2b711170556ef25f963c7748fdc
MD5 825fb4cf00ece7a0f0d1962788ad669b
BLAKE2b-256 4295194f62da7cfb5defe26f1c1d82d2516052d63fad43c978b41fd441ef894a

See more details on using hashes here.

File details

Details for the file json_file_helper-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: json_file_helper-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 4.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.0.1 CPython/3.13.1 Linux/6.12.8-2-cachyos

File hashes

Hashes for json_file_helper-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 055c7077d2d0557ad7116df4077eb8adaa7068f974a5367b035d811e1723b8a4
MD5 e1aa4a1a77f5ebb04af3958d5f7dcea7
BLAKE2b-256 b4f543102a3932f0736adaa8fe46af118798f90c1c8636a6601f07537286604a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page