Skip to main content

mightydatainc-json-surgery

Iterative, AI-guided JSON modification powered by LLM services. Pass in any JSON-compatible object and natural-language instructions. json_surgery breaks the task into discrete atomic operations (assign, delete, append, insert, rename, etc.) that are verified and applied methodically until the object satisfies your instructions.

Installation

pip install mightydatainc-json-surgery

Quick Start

from openai import OpenAI
from mightydatainc_json_surgery import json_surgery

client = OpenAI()

data = {
    "title": "My Report",
    "items": [
        {"id": 1, "status": "draft"},
        {"id": 2, "status": "draft"},
    ],
}

result = json_surgery(
    openai_client=client,
    obj=data,
    modification_instructions='Set the status of every item to "published".',
)
print(result)

JSONSurgeryOptions

All options are optional.

Option Type Description
schema_description str Human-readable schema description passed to the model so it can stay within the expected structure.
skipped_keys list[str] Keys to omit from the placemarked JSON representation shown to the model (e.g. large blobs irrelevant to the task).
on_validate_before_return Callable[[Any], ValidationResult | None] Called before the final object is returned. Return errors to force another round of corrections, or obj_corrected to substitute a fixed version.
on_work_in_progress Callable[[Any], Any | None] Called at the start of each iteration (after the first). Receives the current in-progress object; return a replacement to override it.
give_up_after_seconds int Raise JSONSurgeryError if the process exceeds this many seconds.
give_up_after_iterations int Raise JSONSurgeryError if the process exceeds this many iterations.
from mightydatainc_json_surgery import json_surgery, JSONSurgeryOptions

def validate(obj):
    if not obj.get("title"):
        return {"errors": ["title is required"]}

result = json_surgery(
    openai_client=client,
    obj=data,
    modification_instructions="Remove the 'draft' items and capitalise the title.",
    options=JSONSurgeryOptions(
        schema_description="Object with a 'title' string and an 'items' array.",
        give_up_after_seconds=120,
        give_up_after_iterations=20,
        on_validate_before_return=validate,
    ),
)

JSONSurgeryError

Raised when the process times out or exceeds the iteration limit. The partially-modified object is available on the exception as .obj.

from mightydatainc_json_surgery import json_surgery, JSONSurgeryError

try:
    result = json_surgery(
        openai_client=client,
        obj=data,
        modification_instructions="...",
        options={"give_up_after_iterations": 5},
    )
except JSONSurgeryError as e:
    print("Gave up:", e)
    print("Last known state:", e.obj)

Utility exports

placemarked_json_stringify

Serializes a JSON-compatible object to a string annotated with path comments, the same format shown to the model internally.

from mightydatainc_json_surgery import placemarked_json_stringify

print(placemarked_json_stringify({"a": [1, 2]}, indent=2))
# // root
# {
#   // root["a"]
#   "a": [
#     // root["a"][0]
#     1,
#
#     // root["a"][1]
#     2
#   ]
# }

navigate_to_json_path

Traverses a JSON-compatible object by a path list and returns the parent, key/index, and target.

from mightydatainc_json_surgery import navigate_to_json_path

result = navigate_to_json_path({"items": [{"name": "Alice"}]}, ["items", 0, "name"])
print(result["path_target"])  # "Alice"

Installation and usage

pip install mightydatainc-json-surgery
from mightydatainc_json_surgery import json_surgery

Requires Python >=3.13.

Metadata

Release files for mightydatainc-json-surgery 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for mightydatainc-json-surgery 1.2.2
File Size Uploaded Provenance Actions
mightydatainc_json_surgery-1.2.2.tar.gz 19.5 kB Without provenance Details for mightydatainc_json_surgery-1.2.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for mightydatainc-json-surgery 1.2.2
File Interpreter ABI Platform Actions
mightydatainc_json_surgery-1.2.2-py3-none-any.whl Python 3 none any Details for mightydatainc_json_surgery-1.2.2-py3-none-any.whl

Total release size: 33.0 kB

Release files / mightydatainc_json_surgery-1.2.2.tar.gz

Download URL mightydatainc_json_surgery-1.2.2.tar.gz
Size 19.5 kB
Tags Source
SHA-256 checksum
How to use checksums
3efb3d5ca99251a6284317821c0c71ca45938224622ed5dddb76e0cae8750aa5
BLAKE2b-256 checksum
How to use checksums
0572980747fea95c6a39725c5d8e2dabb03dab00750e15f20c929cb0abd71161
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / mightydatainc_json_surgery-1.2.2-py3-none-any.whl

Download URL mightydatainc_json_surgery-1.2.2-py3-none-any.whl
Size 13.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9cb3fad1e38859c40408650b1766db20db82f3453d2728e79a74173956a658ec
BLAKE2b-256 checksum
How to use checksums
935daa6df8d3d65f390ed0b76b1ac72990b9f3d47f548c25fb79df8b3edec3f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for mightydatainc-json-surgery 1.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page