JsonMend is a robust Ruby gem designed to repair broken or malformed JSON strings. It is specifically optimized to handle common errors found in JSON generated by Large Language Models (LLMs), such as missing quotes, trailing commas, unescaped characters, and stray comments.
Features
-
Lenient Parsing: Handles single quotes, slanted quotes (
“...”), and unquoted keys or values -
LLM Error Correction: Fixes common LLM "hallucinations" like truncated JSON, missing closing brackets/braces, and
...placeholders -
Comment Stripping: Automatically removes JavaScript-style (
//,/* */) and Ruby/Shell-style (#) comments - Flexible Output: Can return either a repaired JSON string or a native Ruby Hash/Array
- Heuristic Repair: Includes advanced logic to merge dangling arrays and handle duplicate keys by splitting objects
Installation
Add this line to your application's Gemfile:
gem 'json_mend'And then execute:
$ bundle installOr install it directly:
$ gem install json_mendUsage
Basic Repair
The primary method is JsonMend.repair. By default, it returns a valid JSON string
require 'json_mend'
# Missing closing brace and unquoted key
broken_json = '{"name": "John", "age": 30, city: "New York"'
JsonMend.repair(broken_json)
# => '{"name":"John","age":30,"city":"New York"}'Return Ruby Objects
If you want to work with the data immediately, pass return_objects: true to get a Hash or Array
result = JsonMend.repair('{"items": [1, 2, 3,]}', return_objects: true)
# => {"items" => [1, 2, 3]}Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release
License
The gem is available as open source under the terms of the MIT License