Parse data from Webhook

Hello,

I’m new to n8n. I’m trying to extract certain data from webhook but I’m can’t get the field value I want.

The data I’m receiving from webhook has this format:

 "body": {
"Doc": "[{'name': 'f90c69a98d', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465',}, {'name': 'b43e7bd05a', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465',}]"

I need to extract the name field value. In the request I can find many names.

Can anybody point me in the right direction?

Thank you for your help.

1 Like

Welcome to the community @streczz

That is probably because the property is Doc is a string, not a JSON. So you have to parse the property Doc to a JSON and then use dot notation to get the values. A function node should do it.

const docs = JSON.parse(items[0].json.body.Doc.replace(/\'/g, '"'))

const results = []

for (const doc of docs) {
results.push({
    json: {
        name: doc.name
   }
})
}

return results
Example workflow
1 Like

Thank you for your reply. Unfortunately I’m getting this:

TypeError: Cannot read property 'replace' of undefined

Sorry this was a typo mistake.

I’m getting this error:

SyntaxError: Unexpected token d in JSON at position 289

This is the full request

[
{
"headers": {
"host": "xxx",
"user-agent": "python-requests/2.25.1",
"accept-encoding": "gzip, deflate",
"accept": "*/*",
"connection": "keep-alive",
"content-type": "application/json",
"content-length": "1235"
},
"params": {
},
"query": {
},
"body": {
"Doc": "[{'name': '31f3bfe7cf', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 20:50:10.529949'}, {'name': '70690477d3', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 20:50:10.529949'}]"
}
}
]

Try it like this:

const docs = JSON.parse(items[0].json.body.Doc.replace(/\'/g, '"').replace(/\, }/g, '}'))

const results = []

for (const doc of docs) {
results.push({
    json: {
        name: doc.name
   }
})
}

return results

Thank you but I’m getting the same error:

SyntaxError: Unexpected token n in JSON at position 267

Can you provide a sample of the data you are working with? Because for me it works fine.

Yes, this is my test workflow

It does not work because that input is different than the one you showed initially. Given that input, the function below should do it, but if you change the input again, it will stop working.

const docs = JSON.parse(JSON.parse(items[0].json.body.Doc)[0].body.Doc.replace(/\'/g, '"').replace(/\, }/g, '}'))


const results = []

for (const doc of docs) {
results.push({
    json: {
        name: doc.name
   }
})
}

return results

Thank you for your reply. The problem is the Webhook because the request is not always the same.

Now I’m getting this request :

"body": {
"Doc": "[{'name': '31f3bfe7cf', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 21:12:05.300580', 'modified_by': 'Administrator', 'parent': 'T-2021-00028', 'parentfield': 'archivos_adjuntos', 'parenttype': 'Task', 'idx': 1, 'docstatus': 0, 'doc_fecha': datetime.date(2021, 10, 19), 'doc_titulo': 'Prueba Again', 'doc_adjunto': '/files/Unknown.jpeg', 'doctype': 'Documentos'}, {'name': '70690477d3', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 21:12:05.300580', 'modified_by': 'Administrator', 'parent': 'T-2021-00028', 'parentfield': 'archivos_adjuntos', 'parenttype': 'Task', 'idx': 2, 'docstatus': 0, 'doc_fecha': datetime.date(2021, 10, 19), 'doc_titulo': 'Prueba 2', 'doc_adjunto': '/files/Unknown.jpeg', 'doctype': 'Documentos'}, {'name': 'e759e30ae0', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 21:12:05.300580', 'modified_by': 'Administrator', 'parent': 'T-2021-00028', 'parentfield': 'archivos_adjuntos', 'parenttype': 'Task', 'idx': 3, 'docstatus': 0, 'doc_fecha': datetime.date(2021, 10, 19), 'doc_titulo': 'Prueba3', 'doc_adjunto': '/files/Unknown.jpeg', 'doctype': 'Documentos'}, {'name': '84370e091a', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 21:12:05.300580', 'modified_by': 'Administrator', 'parent': 'T-2021-00028', 'parentfield': 'archivos_adjuntos', 'parenttype': 'Task', 'idx': 4, 'docstatus': 0, 'doc_fecha': datetime.date(2021, 10, 19), 'doc_titulo': 'Prueba4', 'doc_adjunto': '/files/Unknown.jpeg', 'doctype': 'Documentos'}, {'name': 'f8e8e32ac3', 'owner': 'Administrator', 'creation': '2021-10-19 19:18:58.145465', 'modified': '2021-10-19 21:12:05.300580', 'modified_by': 'Administrator', 'parent': 'T-2021-00028', 'parentfield': 'archivos_adjuntos', 'parenttype': 'Task', 'idx': 5, 'docstatus': 0, 'doc_fecha': datetime.date(2021, 10, 19), 'doc_titulo': 'Prueba5', 'doc_adjunto': '/files/Unknown.jpeg', 'doctype': 'Documentos'}]"
}

I need to understand your function in order to customize it.

With no changes, I’m getting this:

SyntaxError: Unexpected token ' in JSON at position 2

You need to standardize the way you receive data in the webhook node. Otherwise, it’s going to be extremely difficult to develop an algorithm that works with all possible input variations.

Yes.

I think I can standardize. I will try to customize your function.

Thank you very much for your help, much appreciated.