The JsonStructor
library is a Python package that simplifies working with JSON files. It provides methods to read, write, add, remove, and replace elements in a JSON file.
You can install the JsonStructor
library directly from PyPI:
pip install JsonStructor
- Easy reading and writing of JSON files.
- Ability to add new key-value pairs.
- Option to remove existing keys.
- Functionality to replace values for existing keys.
Import the JsonFile
class from the JsonStructor
package and initialize it with the path to your JSON file.
from JsonStructor import JsonFile
json_file = JsonFile('path/to/your/file.json')
To read content from a JSON file, use the get
method.
content = json_file.get()
print(content)
To write content to a JSON file, use the set
method with a dictionary.
data = {'key': 'value'}
json_file.set(data)
To add a new key-value pair to a JSON file, use the add_new_key
method.
json_file.add_new_key('new_key', 'new_value')
To remove an existing key-value pair from a JSON file, use the remove_key
method.
json_file.remove_key('key_to_remove')
The JsonStructor
library includes error handling for common issues such as non-existent files or invalid JSON content.
Each method in the JsonFile
class has detailed docstrings with parameters, return types, and raised exceptions information.
Below are some examples demonstrating how to use the JsonStructor
library:
# Initialize the JsonFile object
json_file = JsonFile('data.json')
# Read data from the file
data = json_file.get()
print(data)
# Initialize the JsonFile object
json_file = JsonFile('data.json')
# Write new data to the file
json_file.set({'new_key': 'new_value'})
# Initialize the JsonFile object
json_file = JsonFile('data.json')
# Add a new key-value pair
json_file.add_new_key('another_key', 'another_value')
# Initialize the JsonFile object
json_file = JsonFile('data.json')
# Remove an existing key
json_file.remove_key('key_to_remove')
For more information and advanced usage, refer to the docstrings provided within the JsonStructor.py
module.
This markdown document can be used as part of your project's README.md or as standalone documentation for users who want to install and use the `JsonStructor` library.