{"id":4163,"date":"2024-12-11T00:59:37","date_gmt":"2024-12-10T15:59:37","guid":{"rendered":"https:\/\/blog.derrylab.com\/?p=4163"},"modified":"2024-12-11T01:00:31","modified_gmt":"2024-12-10T16:00:31","slug":"how-to-fix-broken-json-from-llm-quickly-and-easily","status":"publish","type":"post","link":"https:\/\/blog.derrylab.com\/index.php\/2024\/12\/11\/how-to-fix-broken-json-from-llm-quickly-and-easily\/","title":{"rendered":"How to Fix Broken JSON from LLM Quickly and Easily!"},"content":{"rendered":"\n<p>If you&#8217;ve ever wrestled with broken JSON data from LLMs, you know how frustrating it can be \ud83d\ude2b. Checkout <strong><a href=\"https:\/\/github.com\/mangiucugna\/json_repair\/\" data-type=\"link\" data-id=\"https:\/\/github.com\/mangiucugna\/json_repair\/\" target=\"_blank\" rel=\"noreferrer noopener\">JSON Repair<\/a><\/strong> \ud83d\udee0\ufe0f, a Python module by <a href=\"https:\/\/github.com\/mangiucugna\" data-type=\"link\" data-id=\"https:\/\/github.com\/mangiucugna\" target=\"_blank\" rel=\"noreferrer noopener\">Stefano Baccianella<\/a>, that\u2019s here to save the day! \ud83c\udf1f <\/p>\n\n\n\n<p>This library is specifically crafted to fix invalid JSON, making it perfect for anyone working with LLM outputs \ud83e\udd16 or any dynamic JSON data source.<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Why You\u2019ll Love JSON Repair \u2764\ufe0f<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Effortless Fixes<\/strong>: Repairs common JSON issues like missing commas or unmatched brackets in seconds \u26a1.<\/li>\n\n\n\n<li><strong>AI-Friendly<\/strong>: Especially useful for cleaning up JSON responses from large language models \u2728.<\/li>\n\n\n\n<li><strong>User-Friendly<\/strong>: Easy-to-use Python interface that anyone can master \ud83d\udc0d.<\/li>\n\n\n\n<li><strong>Versatile<\/strong>: Can completely replace <code>json.loads()<\/code> in your projects \ud83d\udee0\ufe0f.<\/li>\n\n\n\n<li><strong>Actively Maintained<\/strong>: Regular updates ensure compatibility and robustness \ud83d\udd04.<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Quick Start Tutorial \ud83d\ude80<\/h4>\n\n\n\n<p>Here\u2019s how easy it is to use JSON Repair:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Install the library:<\/strong> <code>pip install json-repair<\/code><\/li>\n\n\n\n<li><strong>Use it in your Python code:<\/strong> <\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>from json_repair import repair_json\n\nbad_json_string = '{\"name\" \"Derry\", blabiwbwiwabd \"age\": 30, \"abilities\": &#91;\"talk\", \"think\",]'\n\ngood_json_string = repair_json(bad_json_string) print(good_json_string)\n\n# Output: {\"name\": \"Derry\", \"age\": 30, \"abilities\": &#91;\"talk\", \"think\"]}<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Replace <code>json.loads()<\/code> with JSON Repair:<\/strong><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import json_repair<\/code>\n<code>json_string = '{\"name\" \"Derry\", blabiwbwiwabd \"age\": 30, \"abilities\": &#91;\"talk\", \"think\",]'<\/code>\n<code>decoded_object = json_repair.loads(json_string) <\/code>\n<code>print(decoded_object)<\/code>\n<code># Output: {\"name\": \"Derry\", \"age\": 30, \"abilities\": &#91;\"talk\", \"think\"]}<\/code><\/code><\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Repair JSON and get decoded objects directly:<\/strong> <code>import json_repair json_string = '{\"name\": \"ChatGPT\", \"abilities\": [\"talk\", \"think\",]}' decoded_object = json_repair.repair_json(json_string, return_objects=True) print(decoded_object) # Output: {'name': 'ChatGPT', 'abilities': ['talk', 'think']}<\/code><\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code><code>import json_repair<\/code>\n<code><code>json_string = '{\"name\" \"Derry\", blabiwbwiwabd \"age\": 30, \"abilities\": &#91;\"talk\", \"think\",]'<\/code><\/code>\n<code>decoded_object = json_repair.repair_json(json_string, return_objects=True)<\/code>\n<code>print(decoded_object)<\/code>\n<code># Output: {'name': 'ChatGPT', 'abilities': &#91;'talk', 'think']}<\/code><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h4 class=\"wp-block-heading\">Why Wait? <\/h4>\n\n\n\n<p>With JSON Repair, you\u2019ll never have to waste time debugging malformed JSON again. Whether you\u2019re a developer, data scientist, or just someone who deals with JSON daily, this tool will be your best friend \ud83d\udcaa.<\/p>\n\n\n\n<p>Check out the full documentation and latest updates on the <a href=\"https:\/\/github.com\/mangiucugna\/json_repair\">GitHub repository<\/a> \ud83c\udf10. Give it a try today, and say goodbye to JSON headaches forever! \ud83d\ude0e<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you&#8217;ve ever wrestled with broken JSON data from LLMs, you know how frustrating it can be \ud83d\ude2b. Checkout JSON Repair \ud83d\udee0\ufe0f, a Python module by Stefano Baccianella, that\u2019s here to save the day! \ud83c\udf1f This library is specifically crafted to fix invalid JSON, making it perfect for anyone working with LLM outputs \ud83e\udd16 or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4166,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[217,5],"tags":[218,243,167,242,96,64],"class_list":["post-4163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-artificial-intelligence","category-programming","tag-ai","tag-json","tag-library","tag-llm","tag-opensource","tag-tutorial"],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2024\/12\/superai-image-1733846402344.webp?fit=1024%2C1024&ssl=1","jetpack-related-posts":[{"id":4521,"url":"https:\/\/blog.derrylab.com\/index.php\/2025\/06\/17\/how-to-osv-vulnerabilities-api-scanners-and-a-bit-of-hope\/","url_meta":{"origin":4163,"position":0},"title":"How to OSV: Vulnerabilities API, Scanners, and a Bit of Hope","author":"derry","date":"June 17, 2025","format":false,"excerpt":"Security vulnerabilities in open-source dependencies are like background radiation, mostly ignorable, until they're not. Since we are currently working on vulnerability detection research, today, I decided to see how much trouble I could get into by poking around Google's Open Source Vulnerabilities database and its scanner. Spoiler: not much. But\u2026","rel":"","context":"In \"code security tools\"","block_context":{"text":"code security tools","link":"https:\/\/blog.derrylab.com\/index.php\/tag\/code-security-tools\/"},"img":{"alt_text":"pexels-photo-96612.jpeg","src":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/06\/pexels-photo-96612.jpeg?fit=1200%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/06\/pexels-photo-96612.jpeg?fit=1200%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/06\/pexels-photo-96612.jpeg?fit=1200%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/06\/pexels-photo-96612.jpeg?fit=1200%2C800&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/06\/pexels-photo-96612.jpeg?fit=1200%2C800&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":2823,"url":"https:\/\/blog.derrylab.com\/index.php\/2023\/05\/01\/prompt-attack-how-to-leak-system-prompt-in-gpt\/","url_meta":{"origin":4163,"position":1},"title":"Prompt Attack: How to Leak System Prompt in GPT","author":"derry","date":"May 1, 2023","format":false,"excerpt":"What is a Prompt? In the context of natural language processing and machine learning, a prompt refers to the input given to a language model to generate a response. System prompt is the initial prompt that is provided to a language model by a computer program or system. It sets\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","block_context":{"text":"Artificial Intelligence","link":"https:\/\/blog.derrylab.com\/index.php\/category\/artificial-intelligence\/"},"img":{"alt_text":"chatgpt webpage open on smartphone","src":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2023\/05\/pexels-photo-16564261.jpeg?fit=1200%2C900&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2023\/05\/pexels-photo-16564261.jpeg?fit=1200%2C900&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2023\/05\/pexels-photo-16564261.jpeg?fit=1200%2C900&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2023\/05\/pexels-photo-16564261.jpeg?fit=1200%2C900&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2023\/05\/pexels-photo-16564261.jpeg?fit=1200%2C900&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":323,"url":"https:\/\/blog.derrylab.com\/index.php\/2021\/10\/15\/how-to-cut-and-download-youtube-videos-using-command-line\/","url_meta":{"origin":4163,"position":2},"title":"How to Cut and Download Youtube Videos using Command Line","author":"derry","date":"October 15, 2021","format":false,"excerpt":"In some of my free time, I am actively using Instagram, not to show off or seeing people's life that I don't even care about. For me, this social media is perfect for everyday reminders for positive feeds. Usually, people choose a youtube video at the best moment, then re-post\u2026","rel":"","context":"In &quot;linux&quot;","block_context":{"text":"linux","link":"https:\/\/blog.derrylab.com\/index.php\/category\/linux\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2021\/10\/pexels-szabo-viktor-3227986-1.jpg?fit=1200%2C800&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2021\/10\/pexels-szabo-viktor-3227986-1.jpg?fit=1200%2C800&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2021\/10\/pexels-szabo-viktor-3227986-1.jpg?fit=1200%2C800&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2021\/10\/pexels-szabo-viktor-3227986-1.jpg?fit=1200%2C800&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2021\/10\/pexels-szabo-viktor-3227986-1.jpg?fit=1200%2C800&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":5208,"url":"https:\/\/blog.derrylab.com\/index.php\/2025\/12\/31\/how-to-recruit-great-talents-efficiently-with-ai-powered-recruitment-engine\/","url_meta":{"origin":4163,"position":3},"title":"How to Recruit Great Talents Efficiently with AI Powered Recruitment Engine","author":"derry","date":"December 31, 2025","format":false,"excerpt":"I built\u00a0Talens\u00a0because I was tired of how broken hiring feels when you are on the side that actually has to build and lead teams. Before working in industry, I was a\u00a0research lab team leader, responsible for selecting students and researchers who would stay with us for years. Later, as an\u00a0AI\u2026","rel":"","context":"In &quot;Artificial Intelligence&quot;","block_context":{"text":"Artificial Intelligence","link":"https:\/\/blog.derrylab.com\/index.php\/category\/artificial-intelligence\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/12\/image-1.png?fit=1200%2C559&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/12\/image-1.png?fit=1200%2C559&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/12\/image-1.png?fit=1200%2C559&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/12\/image-1.png?fit=1200%2C559&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2025\/12\/image-1.png?fit=1200%2C559&ssl=1&resize=1050%2C600 3x"},"classes":[]},{"id":127,"url":"https:\/\/blog.derrylab.com\/index.php\/2020\/11\/17\/how-to-fix-raspberry-pi-ssh-hangs-or-not-responding\/","url_meta":{"origin":4163,"position":4},"title":"How to Fix Raspberry Pi SSH Hangs or Not Responding","author":"derry","date":"November 17, 2020","format":false,"excerpt":"I just set up a Raspberry PI 4 Model B in the laboratory to automatically connect to the lab's router. I found that each random minutes the SSH is hangs and not responding. Adding IPQoS cs0 cs0 line to the end of \/etc\/ssh\/sshd_config file will fix the issue. :)","rel":"","context":"In &quot;linux&quot;","block_context":{"text":"linux","link":"https:\/\/blog.derrylab.com\/index.php\/category\/linux\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":423,"url":"https:\/\/blog.derrylab.com\/index.php\/2022\/03\/16\/easy-fix-when-pop-os-login-screen-shown-on-the-wrong-monitor\/","url_meta":{"origin":4163,"position":5},"title":"Easy Fix when Pop-OS Login Screen Shown on The Wrong Monitor","author":"derry","date":"March 16, 2022","format":false,"excerpt":"I turned on my newly installed Pop OS and then suddenly the login screen is located on the left monitor. It's horrible! Because I put my left monitor into a vertical position, and the Pop OS login screen orientation is in horizontal mode. Therefore I had to tilt my head\u2026","rel":"","context":"In &quot;linux&quot;","block_context":{"text":"linux","link":"https:\/\/blog.derrylab.com\/index.php\/category\/linux\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2022\/03\/arian-darvishi-wh-RPfR_3_M-unsplash.jpg?fit=1200%2C799&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2022\/03\/arian-darvishi-wh-RPfR_3_M-unsplash.jpg?fit=1200%2C799&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2022\/03\/arian-darvishi-wh-RPfR_3_M-unsplash.jpg?fit=1200%2C799&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2022\/03\/arian-darvishi-wh-RPfR_3_M-unsplash.jpg?fit=1200%2C799&ssl=1&resize=700%2C400 2x, https:\/\/i0.wp.com\/blog.derrylab.com\/wp-content\/uploads\/2022\/03\/arian-darvishi-wh-RPfR_3_M-unsplash.jpg?fit=1200%2C799&ssl=1&resize=1050%2C600 3x"},"classes":[]}],"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/posts\/4163","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/comments?post=4163"}],"version-history":[{"count":1,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/posts\/4163\/revisions"}],"predecessor-version":[{"id":4165,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/posts\/4163\/revisions\/4165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/media\/4166"}],"wp:attachment":[{"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/media?parent=4163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/categories?post=4163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.derrylab.com\/index.php\/wp-json\/wp\/v2\/tags?post=4163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}