Mini-Flow: The "Bulletproof" AI JSON Cleanup Script

Hey Zenphi Community! 🚀

If you are using AI actions (like Gemini, ChatGPT, or Claude) to extract data and pass it into a "Convert JSON to Object" action, you know how powerful it is to route AI insights directly into your workflow schema.

But there is a catch: sometimes, due to heavy server traffic or minor AI glitches, the models fail to return a perfect JSON structure. Instead of clean data, they might wrap the output in markdown backticks (```json) or get stuck in a hallucination loop, leaving trailing garbage (like extra brackets or null values) at the very end of your string.

When that messy text hits your "Convert JSON to Object" action, it crashes the flow with this frustrating error:

Error for Action: Convert JSON to Object. Additional text encountered after finished reading JSON content.

To save you from this headache, we have created a simple, drop-in Cleanup Mini-Flow that automatically sanitizes your AI output before it ever reaches your parsing action.

🛠️ What This Mini-Flow Does

This Mini-Flow contains a smart script that acts as a filter between your AI and your data parser. It automatically:

  • Strips Markdown: Detects and removes any leading ```json or trailing ``` wrappers.

  • Tracks the Structure: Finds the very first { and dynamically counts the brackets to understand the exact shape of your data.

  • Slices the Garbage: The exact millisecond it finds the true matching closing }, it cuts off all trailing text, extra brackets, or random null characters.

  • Delivers Pure JSON: It outputs a perfectly clean JSON string that your "Convert JSON to Object" action can read without failing.

⚙️ How to Implement It

Getting this running in your workspace takes less than a minute. Note: When you open this Mini-Flow, you will see a sample AI action and a sample Convert action. Those are just included as visual examples to help you see the mapping! You only actually need the Script action.

  1. Download & Import: Download the attached Mini-Flow and import it into your Zenphi workspace.

  2. Place the Script: Copy the "Bulletproof JSON Cleanup" script action from the Mini-Flow and paste it right in the middle of your current Gemini/AI action and your existing "Convert JSON to Object" action.

  3. Map the Input: Open the script settings. Pass the Generated Output from your Gemini/AI action into the script's input.

  4. Map the Output: Open your "Convert JSON to Object" action. Instead of using the direct Gemini output, update it to use the Processed Output from the script.

    AI JSON Cleanup_1780921614826.json

Drop this into your workflows and you will never have to worry about random AI formatting errors crashing your automation again.

Happy automating! 🦾