Guides · Web, domains and data
How do I automatically repair malformed JSON in an AI agent?
Send broken JSON to json-repair and get back a valid string and the parsed data. The tool is free to call, and it works on the text alone.
- Python SDK
- Python
Steps
- Install the Strale Python SDK,
straleio. - Create the client with your API key.
- Send the malformed string in the
jsonfield. - Read the repaired string from
output["json"]or the parsed data fromoutput["parsed"].
The solution
Python
# pip install straleio
from straleio import Strale, DoRequest
strale = Strale(api_key="sk_live_YOUR_KEY") # the SDK needs a key, even for free tools
broken_json = """{
name: "John",
age: 30,
"hobbies": ["reading", "coding",],
'active': true
}"""
result = strale.do(DoRequest(
capability_slug="json-repair",
inputs={"json": broken_json},
max_price_cents=5,
))
print(result.output["json"]) # the repaired JSON string
print(result.output["parsed"]) # the same data, already parsed
print(result.output["fixes_applied"]) How it works
Language models often produce malformed JSON: trailing commas, single quotes, unquoted keys, comments and truncated output. The tool repairs these errors with fixed rules, so the same input always gives the same output.
The answer has json (the repaired string), parsed (the same data, parsed, so you do not need a second json.loads), valid and fixes_applied, a list that says what was changed, such as "Added missing closing brackets/braces". Severely malformed input may not be recoverable.
Tools in this guide
| Tool | Price per call |
|---|---|
| JSON Repair | Free |
| URL to Markdown | Free |
| Email Validation | Free |
| DNS Lookup | Free |