JSON - chunhualiao/public-docs GitHub Wiki

pretty-print json with codes

repair before parsing

I use LLMs to generate some json output. But the generated value in json sometimes has double quotes showing up. This breaks later json parsing. I explicitly ask LLMs to avoid generating double quotes. still no effects. How to solve this problem robustly?

This is a common problem when using LLMs for JSON generation. Here are several robust solutions:

Use JSON schema validation with repair

Libraries like jsonschema with repair capabilities can fix common issues:

from json_repair import repair_json

# This library attempts to fix malformed JSON
fixed_json = repair_json(llm_output)
parsed = json.loads(fixed_json)