Extract data from a JSON received from a webhook

I am having trouble extracting data from the JSON below:

[
{
“headers”: {…com.br",
“user-agent”: “python-requests/2.31.0”,
“content-length”: “725”,
“accept”: “/”,
“accept-encoding”: “gzip, deflate”,
“content-type”: “application/json”,
“x-forwarded-for”: “19…41”,
“x-forwarded-host”: “…com.br”,
“x-forwarded-proto”: “https”
},
“params”: {},
“query”: {},
“body”: [
{
“orcamento_informado”: “cpf_cliente”,
“nome_cliente”: “nome_cliente”,
“cpf_cliente”: “numero do cpf”,
“telefone_cliente”: 4198…7,
“contact:”: {
“name”: “nome sistema - teste”,
“number”: “554…0317”
},
“company”: {
“name”: “Nome empresa - sistema”,
“id”: “e84f1191…95”
},
“ticket”: {
“id”: 489…23,
“hash”: “E9413A6B166…9EF390D9137”,
“fields”: [
{
“field”: “email”,
“value”: “cemail”
},
{
“field”: “campanha”,
“value”: “camanha”
},
{
“field”: “origem”,
“value”: “cagem”
},
{
“field”: “link_crm”,
“value”: “camnk”
},
{
“field”: “regiao”,
“value”: “camao”
}
],
“tags”: [
“TA”
]
}
}
],
“webhookUrl”: “https://…”,
“executionMode”: “test”
}
]

When trying to assign the values from the body to a SET node, the variables in the SET node receive NULL, even though the values are correctly coming from the WEBHOOK.

The strangest thing is that when I test it in edit mode, the flow works perfectly, but when we go to production, the values received in the webhook body are not recognized in the flow.

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:

Of course, sorry for my mistake

Hi @agencia_numuu_numuu ,

your JSON above throws a lot of errors when fed into a validator. Have you made sure, you are working with proper syntax?

Best, Ingo

This is the JSON that I’m receiving on my webhook. It is the JSON that the system that I’m integrating send from their api.

I just copied and pasted the JSON. I suppressed some of the variable values, but I believe it doesn’t affect the analysis here.

Follow the JSON again:

item
[
{
“headers”:
{
“host”:
“n8n.xxxxxxxxxx.com.br”,
“user-agent”:
“python-requests/2.31.0”,
“content-length”:
“617”,
“accept”:
/”,
“accept-encoding”:
“gzip, deflate”,
“content-type”:
“application/json”,
“x-forwarded-for”:
“187.xxxxxx.70.xxxxxxx”,
“x-forwarded-host”:
“n8n.xxxxxxxxx.com.br”,
“x-forwarded-proto”:
“https”
},
“params”:
{
},
“query”:
{
},
“body”:
{
“cpf_cliente”:
“034.xxxxxxx.205-98”,
“contact:”:
{
“name”:
“xxxxxx Morais”,
“number”:
“5571xxxxxxxxx301”
},
“company”:
{
“name”:
“Zoxxxxxxxxli”,
“id”:
“e84f11xxxxxxxxa319fa2e395”
},
“ticket”:
{
“id”:
52357,
“hash”:
“9039812373xxxxxxxxxxxF6E214BB3”,
“fields”:
[
{
“field”:
“email”,
“value”:
[email protected]
},
{
“field”:
“campanha”,
“value”:
“[LExxxxxxxxxxBR]”
},
{
“field”:
“origem”,
“value”:
“Busca Paga | Facebook Ads”
},
{
“field”:
“link_crm”,
“value”:
https://crm.rxxxxxxxxxxxxxx0027359ff8?view=pipeline
},
{
“field”:
“regiao”,
“value”:
“Nordeste”
}
],
“tags”:
[
“NORDESTE”,
“interacao”
]
}
},
“webhookUrl”:
https://n8n.xxxxxxxxx.com.br/webhook/3xxxxxxxxxxc9436”,
“executionMode”:
“production”
}
]

Two observations:

  • Your pasted JSON doesn’t show straight double quotes. If you’re working with JSON, make sure you’re using straight double quotes " to avoid syntax errors. My n8n instance could only process the JSON without errors after replacing all the double quotes accordingly. You might want to consider some data cleaning as an intermediate step, if this is what you are getting back from the API.
  • Your screenshot shows a Split Out node before the Edit Fields node. Depending on what fields and how you split, this could also be responsible for generating items with lots unedfined values. You could share your workflow to clarify on that.

I have taken the (adjusted) JSON and sketched out two exemplary paths to further process the body data, one with Split Out, one with Edit Fields only. Let me know if it helps.

Cheers, Ingo

2 Likes

I did exactly the same thing, and whem I try on test mode, it works, but when I put the flow in production, all the variables are read as Undefined in the nodes after the webhook.

follow an example of an execution:

Note that in the node ‘split’ appears an issue, but the flow continues. After that in the node ‘SET’ all the information received from the webhook are missing

Hi @agencia_numuu_numuu ,

you should never ignore the first error message as everything else is likely a consequential error. :sweat_smile:

The expression {{ $json.body[0] }} in your split node cannot work because the body object in your JSON is not an array. It is a standard object, not an array-like structure that can be accessed using an index like [0]. To access specific fields in the body object, you would use the dot (.) notation, depending on your requirements.

If you wanted to include, let’s say cpf_cliente as in your example, you could address the field with a fixed string (see screenshot) or use the expression as such: {{ $json.body.cpf_cliente }}

Cheers, Ingo