LiveJSON Boost Your JSON Handling Efficiency with Versatile Python API

Introduction to livejson

livejson is a lightweight, easy-to-use Python package designed to handle and manipulate JSON data effortlessly. Providing a convenient interface, it simplifies JSON-related tasks, making them faster and more efficient. Whether you’re parsing, creating, updating, or deleting JSON data, livejson offers a plethora of useful APIs to get the job done with minimal code.

Key Features and APIs

1. Loading JSON Data

Load JSON data from a file, string, or URL using the simple load_json API.


from livejson import load_json

# Loading from a file
data = load_json('data.json')

# Loading from a string
json_string = '{"name": "John", "age": 30}'
data = load_json(json_string)

# Loading from a URL
data = load_json('https://api.example.com/data.json')

2. Saving JSON Data

Save JSON data to a file or convert it to a string with the save_json API.


from livejson import save_json

data = {"name": "Jane", "age": 25}

# Saving to a file
save_json(data, 'output.json')

# Converting to a string
json_string = save_json(data)

3. Manipulating JSON Data

Update, delete, or add new elements to your JSON data using straightforward methods.


# Assuming data is a dictionary
data['age'] = 26  # Update

del data['name']  # Delete

data['city'] = 'New York'  # Add new element

4. Querying JSON Data

Use various methods to quickly retrieve data from your JSON structures.


# Retrieving data
age = data.get('age', None)  # Returns 26

App Example using livejson

Let’s build a simple app that manages a JSON-based user database.


from livejson import load_json, save_json

# Load existing user database
users = load_json('users.json')

def add_user(name, age, city):
    user_id = len(users) + 1
    users[user_id] = {"name": name, "age": age, "city": city}
    save_json(users, 'users.json')

def update_user(user_id, name=None, age=None, city=None):
    if user_id in users:
        if name:
            users[user_id]['name'] = name
        if age:
            users[user_id]['age'] = age
        if city:
            users[user_id]['city'] = city
        save_json(users, 'users.json')
    else:
        print("User not found")

def get_user(user_id):
    return users.get(user_id, "User not found")

# Add a new user
add_user('Alice', 28, 'London')

# Update a user
update_user(1, city='Paris')

# Get user information
print(get_user(1))

Using livejson, managing complex JSON data becomes a breeze, allowing you to focus more on your application’s logic rather than data handling.

Hash: a21276119009f0e30c1cf4704c53e9336cc4c174294bb2cec5410c37af29379b

Leave a Reply

Your email address will not be published. Required fields are marked *