Project

json_mend

0.0
No release in over 3 years
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
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 2.3
 Project Readme

JsonMend Ruby Checks

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 install

Or install it directly:

$ gem install json_mend

Usage

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