Skip to main content

Python bindings for the JSON Alchemy C library

Project description

JSON Alchemy

Python bindings for the JSON Alchemy C library - a high-performance JSON processing toolkit.

Features

  • JSON Flattening: Convert nested JSON objects to flat key-value pairs
  • JSON Schema Generation: Generate JSON schemas from JSON objects
  • Batch Processing: Process multiple JSON objects in a single operation
  • Multi-threading: Utilize multiple CPU cores for improved performance

Installation

pip install json-alchemy

Requirements

  • Python 3.8 or higher
  • Python development headers (python3-dev on Linux systems)

The cJSON library is now included directly in the repository, so you don't need to install it separately.

For more detailed installation instructions, see INSTALL.md.

Usage

JSON Flattening

import json
from json_alchemy import flatten_json, flatten_json_batch

# Single object flattening
nested_json = {
    "person": {
        "name": "John",
        "age": 30,
        "address": {
            "city": "New York",
            "zip": "10001"
        }
    }
}

flat_json = flatten_json(nested_json)
print(json.dumps(flat_json, indent=2))
# Output:
# {
#   "person.name": "John",
#   "person.age": 30,
#   "person.address.city": "New York",
#   "person.address.zip": "10001"
# }

# Batch processing
json_objects = [
    {"a": {"b": 1}},
    {"c": {"d": 2}}
]

# Single-threaded batch processing
flattened_batch = flatten_json_batch(json_objects)

# Multi-threaded batch processing (auto thread count)
flattened_batch_mt = flatten_json_batch(json_objects, multi_threaded=True)

# Multi-threaded batch processing (specific thread count)
flattened_batch_mt_spec = flatten_json_batch(json_objects, multi_threaded=True, thread_count=4)

JSON Schema Generation

from json_alchemy import generate_schema, generate_schema_batch

# Single object schema generation
json_obj = {
    "name": "John",
    "age": 30,
    "is_active": True,
    "scores": [85, 90, 78],
    "address": {
        "city": "New York",
        "zip": "10001"
    }
}

schema = generate_schema(json_obj)
print(json.dumps(schema, indent=2))
# Output:
# {
#   "type": "object",
#   "properties": {
#     "name": {"type": "string"},
#     "age": {"type": "integer"},
#     "is_active": {"type": "boolean"},
#     "scores": {
#       "type": "array",
#       "items": {"type": "integer"}
#     },
#     "address": {
#       "type": "object",
#       "properties": {
#         "city": {"type": "string"},
#         "zip": {"type": "string"}
#       }
#     }
#   }
# }

# Batch processing
json_objects = [
    {"a": 1, "b": "string"},
    {"a": 2, "c": True}
]

# Single-threaded batch processing
schema_batch = generate_schema_batch(json_objects)

# Multi-threaded batch processing (auto thread count)
schema_batch_mt = generate_schema_batch(json_objects, multi_threaded=True)

# Multi-threaded batch processing (specific thread count)
schema_batch_mt_spec = generate_schema_batch(json_objects, multi_threaded=True, thread_count=4)

Performance Considerations

Based on our benchmarks:

  1. Batch Processing:

    • For small to medium batches (10-1000 objects), batch processing provides significant speedups.
    • For larger batches (5000+ objects), consider splitting them into smaller batches.
  2. Multi-threading:

    • Multi-threading provides significant speedups for medium-sized batches (around 1000 objects).
    • For very small batches, the overhead of thread creation may outweigh the benefits.
  3. Optimal Batch Size:

    • The optimal batch size for flattening operations is around 1000 objects.
    • The optimal batch size for schema generation is around 10-100 objects.

Building from Source

First, ensure you have the cJSON library installed (see Requirements above).

# Clone the repository
git clone https://github.com/amaye15/AIMv2-rs.git
cd AIMv2-rs/python

# Install the package in development mode
pip install -e .

If you encounter any issues during installation, please refer to the INSTALL.md file for troubleshooting.

License

MIT License

Links

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

aimv2_json_alchemy-1.3.1.tar.gz (36.7 kB view details)

Uploaded Source

File details

Details for the file aimv2_json_alchemy-1.3.1.tar.gz.

File metadata

  • Download URL: aimv2_json_alchemy-1.3.1.tar.gz
  • Upload date:
  • Size: 36.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.10

File hashes

Hashes for aimv2_json_alchemy-1.3.1.tar.gz
Algorithm Hash digest
SHA256 09ff7937b4986bb58c90aa42e269454bcabe1b55257ff652c69f0b15e3f16f25
MD5 c8f218134f7aaaf3764a4390b6dc309f
BLAKE2b-256 9049320eb446b5e30769f7658d600b1811d5a2153c58af12a2a3b82e89a31c41

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