Get the JSON data from String

Describe the problem/error/question

I have data from a python script that is supposed to give me data in JSON format. The script sometimes throws warning messages. To cater for this I need to find the start of the ‘[{“title:”’ so I can get the rest of the data and parse as JSON.

What is the error message (if any)?

I get this error ERROR: Code doesn’t return items properly
Please return an array of objects, one for each item you would like to output

Please share your workflow

Share the output returned by the last node

‘<’ not supported between instances of ‘datetime.datetime’ and ‘float’ Error trying to connect to socket: closing socket - [Errno -2] Name or service not known Error trying to connect to socket: closing socket - [Errno -2] Name or service not known [{“title”: “blah blah blah blah”,“date”: “2023-07-03”, “datetime”: “2023-07-03 17:45:32.809182”}]

Information on your n8n setup

  • **n8n version:n8n-nodes-base.code
  • Database (default: SQLite):
  • n8n EXECUTIONS_PROCESS setting (default: own, main):
  • Running n8n via (Docker):
  • **Operating system:ubuntu-22-04

Hi @Thobani_Mngoma :wave: Welcome to the community! :tada:

Taking a look here, we’d likely need more information to help you out - can you provide the exact data you’re sending to the code node when running into the error, and what you’re expecting to see as output instead of the error you’re getting? Obviously you can redact or change any potentially sensitive data :+1:

The error looks like you’re trying to compare incompatible data types, but otherwise it’s hard to tell what’s happening here.

1 Like

Hi @EmeraldHerald, Thank you for the warm welcome. I did attach data snippet on the original. Please find the below extended dataset. If you see it I want to clean the data so I do not include unformated data. I only want data that is JSON format.

“‘<’ not supported between instances of ‘datetime.datetime’ and ‘float’ Error trying to connect to socket: closing socket - [Errno -2] Name or service not known Error trying to connect to socket: closing socket - [Errno -2] Name or service not known [{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, “domain_status”: ”Veriifed”, “Country”:”USA”},{“title”: “blah blah blah blah blah blah blah”,”date”: “4 hours ago”, “datetime”: “2023-07-09 17:45:32.809182”, “domain_age”: “26 years”, “cert_issuer”: “Let’s Encrypt”, "domain_status”: ”Veriifed”, “Country”:”USA”}]

Hello @EmeraldHerald ,

I found the answer. But please do share your version of the answer, I would love to learn and grow.

const currentValue = $input.first().json.stdout;

let accumulator = [];
const startIndex = currentValue.indexOf(‘[{’);
accumulator.push(…JSON.parse(currentValue.substring(startIndex)));

return accumulator;

1 Like

Looks good to me, @Thobani_Mngoma - glad you got this sorted :raised_hands:

1 Like

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