Skip to main content

A simple, efficient, and feature-rich Python library for working with JSON files.

Project description

NanoJson Library by Mohammed Ghanam

PyPI - Version
Python
License
Status
PyPI - Downloads

NanoJson is a simple, efficient, and feature-rich Python library for working with JSON files.
It provides methods for reading, writing, updating, and manipulating JSON data.
Whether you're working with simple JSON files or deeply nested structures, NanoJson makes it easy to work with your data.


🚀 Installation

You can install NanoJson from PyPi using pip:

pip install NanoJson

📖 Usage

Importing the Library

To use NanoJson, simply import it like this:

import NanoJson

Creating a NanoJson Object

To work with a JSON file, create an instance of NanoJson by providing the file path and optional indentation level:

json_handler = NanoJson('data.json', indent=4)

Methods

➡️ Read JSON Data

You can read the data from the JSON file either as a dictionary or in a pretty-printed format:

data = json_handler.read_json()  # Returns the data as a dictionary
pretty_data = json_handler.read_json(pretty=True)  # Pretty-printed JSON string

➡️ Write JSON Data

To write data to the JSON file:

json_handler.write_json({"name": "John", "age": 30})

➡️ Update JSON Data

You can update an existing key or add a new key to the JSON file:

json_handler.update_json("city", "New York")

➡️ Delete a Key

To delete a key from the JSON file:

json_handler.delete_key("age")

➡️ Search for a Key

Search for a key and get its value:

value = json_handler.search_key("name")

➡️ Deep Search for Nested Keys

Perform a deep search for a key even if it's nested inside other objects or lists:

value = json_handler.deep_search("address.street")

➡️ Append to a List

You can append an element to an existing list inside the JSON file:

json_handler.append_to_list("hobbies", "reading")

➡️ Remove from a List

To remove an element from a list:

json_handler.remove_from_list("hobbies", "reading")

➡️ Merge JSON Data

Merge new data into the existing JSON file:

json_handler.merge_json({"country": "USA", "state": "California"})

➡️ Clear JSON Data

To clear all data in the JSON file:

json_handler.clear_json()

➡️ Backup and Restore

You can back up your JSON file and restore it from a backup:

json_handler.backup_json("backup.json")
json_handler.restore_backup("backup.json")

💡 Example

Here’s a complete example of how to use NanoJson:

import NanoJson

# Initialize NanoJson
json_handler = NanoJson('data.json')

# Write data
json_handler.write_json({'name': 'John', 'age': 25})

# Update data
json_handler.update_json('city', 'New York')

# Search for a value
print(json_handler.search_key('name'))

# Append to a list
json_handler.append_to_list('hobbies', 'reading')

# Remove from a list
json_handler.remove_from_list('hobbies', 'reading')

# Deep search
print(json_handler.deep_search('city'))

# Merge new data
json_handler.merge_json({'country': 'USA'})

# Rename a key
json_handler.rename_key('city', 'location')

# Backup and restore
json_handler.backup_json('backup.json')
json_handler.restore_backup('backup.json')

# Get file size
print(json_handler.get_size())

🌟 Features

✅ Read, write, and update JSON files easily.
✅ Deep search for nested keys.
✅ Append and remove elements from lists inside JSON.
✅ Backup and restore your JSON data.
✅ Efficient error handling with descriptive error messages.
✅ Supports nested structures and complex data types.
✅ Works with pretty-printed JSON strings.
✅ File size information and other utility functions.
✅ Backup and restore capabilities.


🛡️ Error Handling

In case of errors, NanoJson will return detailed error messages including the type of error and the line number where it occurred.
This helps developers easily identify and resolve issues in their code.

For example, if a file is not found:

data = json_handler.read_json()
if isinstance(data, str) and "Error" in data:
    print(f"Error: {data}")

📄 License

This project is licensed under the MIT License. See the LICENSE file for more details.


💬 Support

If you encounter any issues or have questions, feel free to open an issue on the repository or contact the author.

Author: Mohamed Ahmed Ghanam


NanoJson – A simple and powerful JSON handler for Python.

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

nanojson-1.3.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

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

nanojson-1.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file nanojson-1.3.tar.gz.

File metadata

  • Download URL: nanojson-1.3.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanojson-1.3.tar.gz
Algorithm Hash digest
SHA256 0c31bb53acba3342257b82fd9a52252a322ce9e5cbdda30566aaa56a22635656
MD5 3033d5b1ede8d23ce1d17b3adbd8d741
BLAKE2b-256 9e3b1effb171077b3c70e0476b9b714f61bf1d9d565677cc8773742ef18a47e0

See more details on using hashes here.

File details

Details for the file nanojson-1.3-py3-none-any.whl.

File metadata

  • Download URL: nanojson-1.3-py3-none-any.whl
  • Upload date:
  • Size: 5.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for nanojson-1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 31d0a07869a50c199cfddb01273231ec9a0907898fea755d7e8f01df2ae76148
MD5 77bae503af6f94c52216f18f1cfa53df
BLAKE2b-256 63ba11a51945249e284ef6a8f7972d3ab29309e28aaf9fd04e205092906791ff

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