Json body working in Make and not N8N

Describe the problem/error/question

Hi,

I’m getting a json from an HTTP call and extracting some values from it to create a new json for a webhook response.

The webhook response is properly setup and works with some other code.

Right now I have a code module that takes the first json and transform it into an array. I tried to turn it in a json in various ways.
My closest solution (below) so far seems good… But a bracket is added at the end of the input item when I try to use it in the webhook response.

I feel stuck right now and can’t seem to figure it out. What am I doing wrong? Or is there a bug?

The ideal webhook response would look like:

[
    {
        "id": "0a61l",
        "block": "form",
        "inputs": [
            {
                "type": "select",
                "name": "A select",
                "label": "Owner",
                "selectValues": [
                    {
                        "name": "My first value",
                        "value": "my-first-value"
                    },
                    {
                        "name": "My second value",
                        "value": "my-second-value"
                    },
                    {
                        "name": "07q6h96c0671vjq4",
                        "value": "07q6h96c0671vjq4"
                    },
                    {
                        "name": "tw5947pt1fop5t",
                        "value": "tw5947pt1fop5t"
                    },
                    {
                        "name": "vb5qtk37fmk31v1u",
                        "value": "vb5qtk37fmk31v1u"
                    },
                    {
                        "name": "eyuzd4nv34e17db2",
                        "value": "eyuzd4nv34e17db2"
                    },
                    {
                        "name": "osg53hexfy1w9n1",
                        "value": "osg53hexfy1w9n1"
                    },
                    {
                        "name": "poz1nrd1pe1a7b9",
                        "value": "poz1nrd1pe1a7b9"
                    },
                    {
                        "name": "8h21271ae2ed9",
                        "value": "8h21271ae2ed9"
                    },
                    {
                        "name": "mu1g5xfrormjllg",
                        "value": "mu1g5xfrormjllg"
                    },
                    {
                        "name": "y6hyevbo8j960y2l",
                        "value": "y6hyevbo8j960y2l"
                    },
                    {
                        "name": "v6xp3tatg419jom",
                        "value": "v6xp3tatg419jom"
                    },
                    {
                        "name": "yl7vowutfypcor",
                        "value": "yl7vowutfypcor"
                    }
                ]
            }
        ],
        "actionButton": {
            "name": "My Button",
            "label": "My Action Button",
            "style": "secondary"
        },
        "url": "http://callback.url.com"
    }
]

What is the error message (if any)?

“ERROR: Invalid JSON in ‘Response Body’ field
Check that the syntax of the JSON in the ‘Response Body’ parameter is valid”

(and that’s about right!)

Please share your workflow

(Select the nodes on your canvas and use the keyboard shortcuts CMD+C/CTRL+C and CMD+V/CTRL+V to copy and paste the workflow.)

Share the output returned by the last node

The json body is incorrect because I have one bracket too many here :
“value”: “yl7vowutfypcor”}}] (code below)

What’s weird is that the json from the last scenario seems fine when I look at it from the left side panel… But using {{ $json.inputs }} seems to add a } for some reason.

Here is the output of the last node before the webhook response:

[
  {
    "id": "elmjw149",
    "block": "form",
    "inputs": [
      {
        "type": "select",
        "name": "A select",
        "label": "Owner",
        "selectValues": [
          {
            "name": "My first value",
            "value": "my-first-value"
          },
          {
            "name": "My second value",
            "value": "my-second-value"
          },
          {
            "name": "07q6h96c0671vjq4",
            "value": "07q6h96c0671vjq4"
          },
          {
            "name": "tw5947pt1fop5t",
            "value": "tw5947pt1fop5t"
          },
          {
            "name": "vb5qtk37fmk31v1u",
            "value": "vb5qtk37fmk31v1u"
          },
          {
            "name": "eyuzd4nv34e17db2",
            "value": "eyuzd4nv34e17db2"
          },
          {
            "name": "osg53hexfy1w9n1",
            "value": "osg53hexfy1w9n1"
          },
          {
            "name": "poz1nrd1pe1a7b9",
            "value": "poz1nrd1pe1a7b9"
          },
          {
            "name": "8h21271ae2ed9",
            "value": "8h21271ae2ed9"
          },
          {
            "name": "mu1g5xfrormjllg",
            "value": "mu1g5xfrormjllg"
          },
          {
            "name": "y6hyevbo8j960y2l",
            "value": "y6hyevbo8j960y2l"
          },
          {
            "name": "v6xp3tatg419jom",
            "value": "v6xp3tatg419jom"
          },
          {
            "name": "yl7vowutfypcor",
            "value": "yl7vowutfypcor"
          }
        ]
      }
    ],
    "actionButton": {
      "name": "My Button",
      "label": "My Action Button",
      "style": "secondary"
    },
    "url": "http://callback.url.com"
  }
]

And “output” (what I see in the Response Body module):

[
    {
        "id": "0a61l",
        "block": "form",
        "inputs": [{"type": "select", "name": "A select", "label": "Owner", "selectValues": [{"name": "My first value", "value": "my-first-value"},{"name": "My second value", "value": "my-second-value"},{"name": "07q6h96c0671vjq4", "value": "07q6h96c0671vjq4"},{"name": "tw5947pt1fop5t", "value": "tw5947pt1fop5t"},{"name": "vb5qtk37fmk31v1u", "value": "vb5qtk37fmk31v1u"},{"name": "eyuzd4nv34e17db2", "value": "eyuzd4nv34e17db2"},{"name": "osg53hexfy1w9n1", "value": "osg53hexfy1w9n1"},{"name": "poz1nrd1pe1a7b9", "value": "poz1nrd1pe1a7b9"},{"name": "8h21271ae2ed9", "value": "8h21271ae2ed9"},{"name": "mu1g5xfrormjllg", "value": "mu1g5xfrormjllg"},{"name": "y6hyevbo8j960y2l", "value": "y6hyevbo8j960y2l"},{"name": "v6xp3tatg419jom", "value": "v6xp3tatg419jom"},{"name": "yl7vowutfypcor", "value": "yl7vowutfypcor"}}]
            }
        ],
        "actionButton": {
            "name": "My Button",
            "label": "My Action Button",
            "style": "secondary"
        },
        "url": "http://callback.url.com"
    }
]

Information on your n8n setup

  • n8n version: Cloud, so last I guess
  • Database (default: SQLite): default
  • n8n EXECUTIONS_PROCESS setting (default: own, main): Unsure
  • Running n8n via (Docker, npm, n8n cloud, desktop app): Cloud
  • Operating system: Not relevant… but windows

Please let me know if something else is needed to solve this.

I’ll add the workflow code again since I apparently failed before.

Go it to work through a work around… But even with a proper body that works in Make, it still doesn’t work in N8N

Here is a body example

[
    {
        "id": "0a61l",
        "block": "form",
        "inputs": [{
                "type": "select",
                "name": "A select",
                "label": "Owner",
                "selectValues": [{"value": "07q6h96c0671vjq4", "name": "Aliénor"},{"value": "tw5947pt1fop5t", "name": "Benjamin"},{"value": "vb5qtk37fmk31v1u", "name": "Bodo"},{"value": "eyuzd4nv34e17db2", "name": "Clarence"},{"value": "osg53hexfy1w9n1", "name": "Dylan"},{"value": "poz1nrd1pe1a7b9", "name": "Dylan test"},{"value": "8h21271ae2ed9", "name": "Elliot"},{"value": "mu1g5xfrormjllg", "name": "John Doe"},{"value": "y6hyevbo8j960y2l", "name": "Keyvan"},{"value": "v6xp3tatg419jom", "name": "Mathilde"},{"value": "yl7vowutfypcor", "name": "Yoann"}]
            }
        ],
        "actionButton": {
            "name": "My Button",
            "label": "My Action Button",
            "style": "secondary"
        },
        "url": "http://callback.url.com"
    }
] 

I get a ERROR: Invalid JSON in ‘Response Body’ field… only in N8N for this same exact code.

hello @Elliot_Boucher

I don’t know about make, maybe it is less strict to JSON, but the provided JSON in the Response to the webhook is not a valid one :slight_smile:
You can test it here: https://jsoneditoronline.org/

What are you trying to achieve? The Code node seems very overloaded

@barn4k According to @Elliot_Boucher latest provided JSON and this linter. It is quite valid.

The initially provided example is not valid tho.

1 Like

Yep, it’s a valid one and yet… Can’t send it through a webhook response or a HTTP call in N8N.

Quite confusing.

Edit : After testing, I can confirm the json is correct.
However, it only work if I copy paste it, not if the values come from a previous node with {{ $json.items }} replacing some variables (same body in the end in principle…)

Here is an example with an HTTP node that works with the body copy/pasted and an HTTP call with the {{ $json.items }} … Not working

if $json.items is an array, aren’t the additional brackets [ ] an issue?
grafik

PS: sometimes it helps to covert it to an string with JSON.stringify($json.items)

2 Likes

Got it to work like this

[
    {
        "id": "0a61l",
        "block": "form",
        "inputs": [{
                "type": "select",
                "name": "A select",
                "label": "Owner",
                "selectValues": {{ JSON.stringify($json["items"]) }}
            }
        ],
        "actionButton": {
            "name": "My Button",
            "label": "My Action Button",
            "style": "secondary"
        },
        "url": "http://callback.url.com"
    }
]

Thanks!

1 Like

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