Format JSON response from open AI

Describe the problem/error/question

I’m using the openAI for image processing but it is not providing me the valid json response. I have already mentioned this in prompt but still not providing the valid json.

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

[
  {
    "content": "```json\n{\n  \"trends\": {\n    \"leading_country\": \"United States\",\n    \"lowest_count_countries\": [\"Australia\", \"Czech Republic\"],\n    \"general_trend\": \"The United States has significantly more counts compared to other countries, with a large drop-off to the second-ranked country, India. The rest of the countries have similar, lower counts.\"\n  },\n  \"axes\": {\n    \"x_axis\": \"Country\",\n    \"y_axis\": \"Counts (No specific label for this axis, inferred based on bars and numbers next to them)\"\n  },\n  \"key_points\": {\n    \"outliers\": {\n      \"high\": {\n        \"country\": \"United States\",\n        \"count\": 16\n      },\n      \"low\": {\n        \"countries\": [\n          {\n            \"country\": \"Australia\",\n            \"count\": 1\n          },\n          {\n            \"country\": \"Czech Republic\",\n            \"count\": 1\n          }\n        ]\n      }\n    }\n  },\n  \"labels\": {\n    \"graph_title\": \"Africa Green Race\",\n    \"section_title\": \"October Countdown\",\n    \"time_remaining\": {\n      \"days\": \"7\",\n      \"hours\": \"10\",\n      \"minutes\": \"41\",\n      \"seconds\": \"45\"\n    },\n    \"countries\": [\"United States\", \"India\", \"Canada\", \"Switzerland\", \"Australia\", \"Czech Republic\"]\n  },\n  \"graph"
  }
]

It looks like your topic is missing some important information. Could you provide the following if applicable.

  • n8n version:
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker, npm, n8n cloud, desktop app):
  • Operating system:

Hi @shubham_akodiya

Thanks for posting here and welcome to the community! :partying_face:

If your output always has this pattern, you could just transform it with something like:

{{ JSON.parse($json.content.slice(8).slice(0,-10).replace(/\s/g, '').replace(/\n/g, '') + '}') }}

However, I think you could get better (and more stable) results by adding the Structured Output Parser node to do the formatting for you.

Here’s an example for your use case:

Let us know how you get on! :raised_hands:

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.