Persistent difficulties with JSON formatting in n8n

Hello everyone,

I’m experiencing increasing difficulties with data formatting between nodes in my n8n workflow. I’ve noticed that the outputs generated by certain nodes (particularly those involving AI APIs like ChatGPT) are consistently in a format that is becoming increasingly complex to normalize.

The problem in detail

The outgoing data from my nodes often appears as strings containing incomplete or poorly structured JSON. For example, I receive structures like:

{
  "choices": [
    {
      "message": {
        "content": "[\n  {\n    \"json\": {\n      \"document_analysis\": { ... }"
      }
    }
  ]
}

Where the content itself is a truncated JSON, encapsulated as a string instead of a proper JSON object.

Solutions attempted without success

I’ve tried several approaches:

  1. Using Code nodes to parse and restructure the data
  2. Employing Function nodes to transform the format
  3. Attempting to use JSON Parse on different parts of the data
  4. Creating complex expressions in Set nodes

But all these approaches fail in similar ways, with errors like “Code doesn’t return items properly”.

Fundamental question

Does n8n have a recommended method for handling these nested and potentially incomplete formats? Are there best practices for normalizing data between nodes, particularly when they come from AI APIs that return JSON as text?

I feel that what should be a relatively simple task (extracting and restructuring JSON data) is becoming exponentially complex in my current workflow.

Any help or advice would be greatly appreciated.

Thank you!

Do you have an example workflow to share in a code block so we can have a look. This just looks like a simple stringified json object which you’ll need to parse.