Skip to main content

Manipulate JSON data or dict values as attributes of an object.

Project description

JSON Handler

It's easy-to-use library which helps you to work with data in JSON files and Python dictionaries like if they were Python objects.

Installation

pip install json_handler

No dependencies!

Getting Started

Example 1.

You can easily read existing JSON file and modify it. Here is JSON file (example.json):

{
    "field_1": "Hi",
    "field_2": "Hello world!",
    "field_3": {
        "sub_field": 123
    }
}

We can modify the content of that by using following code:

from json_handler import JsonHandler

handler = JsonHandler('example.json')

handler.field_1 = 123
handler['field_2'] = "What's up?"
handler.field_3 = {}
handler.field_3.sub_field = [1, 2, 3]

handler.save()

The result of modifications will be (example.json):

{
    "field_1": 123,
    "field_2": "What's up?",
    "field_3": {
        "sub_field": [1, 2, 3]
    }
}

Example 2.

If file does not exist, it will be automatically created after using save() method.

from json_handler import JsonHandler

handler = JsonHandler('example2.json')

handler.a = 5
handler.b = 'Hi there'
handler.save()

In the same directory file (example2.json) will be created with following content:

{
  "a": 5,
  "b": "Hi there"
}

Example 3.

There is way to automatically save all changes happening with data. To do that you should just set parameter 'auto_save' to True.

from json_handler import JsonHandler

handler = JsonHandler('example3.json', auto_save=True)

handler.hi = 'Hello'
handler.five = 5

In the same directory file (example3.json) will be created with following content :

{
  "hi": "Hello",
  "five": 5
}

Example 4.

You can use Python built-in dict methods with JsonHandler object. For example:

from json_handler import JsonHandler

handler = JsonHandler('example4.json', auto_save=True)

handler.hi = 'Hello'
handler.five = 5
handler.sub_dict = {}
handler.sub_dict.fine = 'ok'

print(handler.keys())
print(handler.values())
print(handler.items())

The output will be:

dict_keys(['hi', 'five', 'sub_dict'])
dict_values(['Hello', 5, {'fine': 'ok'}])
dict_items([('hi', 'Hello'), ('five', 5), ('sub_dict', {'fine': 'ok'})])

Also you can clear data by using dict.clear() method:

handler.clear()
print(handler)

The output will be:

{}

Yeah, you can actually print JsonHandler object and it will be printed like usual Python dict

Example 5.

You can pretty print your JsonHandler object like any dict object by using built-in python module pprint.

from pprint import pprint
from json_handler import JsonHandler


handler = JsonHandler()
handler.well = [{'hi': 'hello'} for _ in range(5)]

pprint(handler)

The output will be:

{'well': [{'hi': 'hello'},
          {'hi': 'hello'},
          {'hi': 'hello'},
          {'hi': 'hello'},
          {'hi': 'hello'}]}

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_handler-2.0.1.tar.gz (5.4 kB view details)

Uploaded Source

File details

Details for the file json_handler-2.0.1.tar.gz.

File metadata

  • Download URL: json_handler-2.0.1.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/30.0 requests/2.26.0 requests-toolbelt/0.9.1 urllib3/1.26.2 tqdm/4.61.1 importlib-metadata/4.8.1 keyring/23.2.1 rfc3986/1.5.0 colorama/0.4.4 CPython/3.8.6

File hashes

Hashes for json_handler-2.0.1.tar.gz
Algorithm Hash digest
SHA256 de5a03f188bb4e27c22b2911c65caed30e6595605612792899a8f2ea40cdd002
MD5 0cdeff1a3bdbd9eaccc9c793d0b4da94
BLAKE2b-256 64a6e1cde610ed1b654d9c2911573da2650396ca5005ffe634fde6f4e9d6660e

See more details on using hashes here.

Supported by

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