Is it possible for webhook to return json object instead of array?

Does n8n only response in json array [] and can it return as json object {}? I tried pipedream webhook and it returned as json object. I’m working with a rigid system that expecting json object. When working with n8n webhook the system encountered error below due to json array.

What is the error message (if any)?

Failed to parse the API response: 'Cannot deserialize the current JSON array 
(e.g. [1,2,3]) into type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' 
because the type requires a JSON object (e.g. {\"name\":\"value\"}) to deserialize correctly.\r\n
To fix this error either change the JSON to a JSON object (e.g. {\"name\":\"value\"}) or change 
the deserialized type to an array or a type that implements a collection interface 
(e.g. ICollection, IList) like List<T> that can be deserialized from a JSON array. 
JsonArrayAttribute can also be added to the type to force it to deserialize from a JSON array.\r\n
Path '', line 1, position 1.'"
    }

I tried to edit the output by removing the bracket but the bracket reinjected instantly upon saved.

Information on your n8n setup

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

With pipedream the response can be as json object rather than array.

Thanks to @BramKn , the answer is yes when using Respond to Webhook node.

Ignore the output in the editor as it always wrapped it with []

the actual response is without [].

1 Like

Kinda weird, I get inconsistency result, since the first success run now I constantly getting this as output.
image

Apparently it only give me the json response when the webhook is set to “When Last Node Finishes”.

This is contradicting to my other question regarding responding in XML as I couldn’t use the same webhook to return the result I wanted :cry:

To answer your question. Yes, you can respond literally in any way you want, you just have to configure it accordingly.

Honestly I am a little bit configured right now. What kind of response do you want, a JSON object or XML? We can then post a simple example.

Hi @jan , I want my webhook to handle both scenarios but it isn’t possible, using the workflow you can try it, and it need to tweak the Webhook response option to get the correct response of another. I’m on 0.226.2.

to get XML

http://localhost:5678/webhook-test/9e5f85fe-dec9-4880-ba24-3f7b4c41b0ae

to get json object

http://localhost:5678/webhook-test/9e5f85fe-dec9-4880-ba24-3f7b4c41b0ae?OBID=123123123&CombineRels=Classification_21,DocumentRevisions_12

The problem is that you configured your Webhook Node with: Respond: When Last Node Finishes" and you then used the “Respond to Webhook Node” instead of configuring the Webhook Node to us the “Respond to Webhook Node” by setting it to: Respond: Using "Respond to Webhook Node".

Additionally did also the response data did not get stringified.

Here is the updated workflow:

2 Likes

Thanks for the updated workflow. It works :clap:

However it makes me wonder without changing the workflow but just webhook response option could result in a vast behavior that “when last node finishes” works without stringify while “using webhook respond node” requires stringify.

That depends on the options.
If you use the same options (no matter if configured on Webhook Node or Respond to Webhook) that it should simply return the data of the node/last node, then n8n takes care of the logic and makes sure the data is returned correctly.
If you configure the Resond to Webhook Node to return specific data, in your above example “Text” then it is on the user to make sure that the data, which is defined in that parameter, is formatted correctly. And in your first example, you mixed strings with objects and that is what JavaScript does in that case, that it returns an object as [object Object].

Thanks Jans for taking time to solve this for me and with the additional explanation :+1:, it makes sense now looking at my previous configuration and the output I got. :slight_smile:

1 Like

Glad to hear. There are for sure things in n8n which are not completely obvious in the beginning, and we are constantly trying to make things easier and clearer. But generally, if there is a choice between simplicity and power & flexibility will we choose power & flexibility. Because we do not want to be the most simple tool out there, we rather want to be the most powerful one which sadly also means that it is sometimes harder for people that start to use n8n.

Have fun!

1 Like

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