JsonRemedy is an Elixir-native JSON repair library that intelligently fixes malformed JSON strings. It leverages Elixir’s binary pattern matching and functional composition for superior performance and elegant design.
Motivation
Large Language Models and other AI systems often produce almost valid JSON. JsonRemedy is designed to repair these issues while preserving the intended data structure, making it ideal for robust JSON handling in Elixir applications dealing with AI model outputs, legacy systems, or unreliable data sources.
Key Features
- Elixir-Native Performance: Uses binary pattern matching for speed.
- Intelligent Repair: Context-aware fixes for various JSON syntax and structural issues.
- Blazing Fast: Leverages BEAM optimizations.
- Detailed Logging: Optional repair action tracking.
- Functional Design: Immutable, composable, and testable.
- Multiple Strategies: Choose from different parsing approaches based on your needs.
Repair Capabilities
JsonRemedy can fix a wide range of issues, including:
- Syntax Fixes: Missing quotes, unquoted keys, single quotes, trailing commas, missing commas.
- Structure Repairs: Incomplete objects or arrays, missing colons.
- Value Corrections: Boolean variants (e.g.,
Truetotrue), null variants, unquoted strings. - Content Cleaning: Removes code fences (e.g.,
```json), comments, and extra surrounding text.
Quick Start
Add json_remedy to your mix.exs dependencies:
def deps do
[
{:json_remedy, "~> 0.1.0"}
]
end
Basic Usage
# Repair and parse in one step
malformed_json = """
{
name: "Alice",
age: 30,
active: True
}
"""
{:ok, data} = JsonRemedy.repair(malformed_json)
# => %{"name" => "Alice", "age" => 30, "active" => true}
# Get the repaired JSON string
{:ok, fixed_json} = JsonRemedy.repair_to_string(malformed_json)
# => "{\"name\":\"Alice\",\"age\":30,\"active\":true}"
How It Works: The Elixir Advantage
JsonRemedy leverages Elixir’s strengths by using hyper-optimized binary pattern matching, allowing for 10-100x faster processing than traditional character iteration and providing zero-copy binary operations. It also features composable repair functions for modularity and offers multiple parsing strategies including binary pattern matching, parser combinators, and stream processing.
Performance Benchmarks
JsonRemedy delivers exceptional performance:
- 4.32M ops/sec for valid JSON parsing.
- 90,000+ ops/sec for malformed JSON repair.
- < 8KB peak memory usage for repairs.
It significantly outperforms the original Python json-repair library in speed and memory efficiency.
Real-World Use Cases
JsonRemedy is ideal for:
- LLM Integration: Extracting data from AI responses that often contain malformed JSON.
- Data Pipeline Healing: Processing data from external APIs or unreliable sources.
- Config File Recovery: Loading configuration files that might have minor syntax errors.
Contributing
We welcome contributions! JsonRemedy is designed to be modular, testable, extensible, and performant. Check the GitHub repository for development setup instructions and guidelines for adding new repair rules.
License
JsonRemedy is released under the MIT License.






















