JSON output from HTTP Request is not getting parsed

Describe the problem/error/question

I am getting an output JSON from an HTTP Request, but I just cant seem to find a way of parsing it into a JSON so I can save it to a database. I have read dozens of articles, but havent found anyone with a similar problem. Tried all snippets shared on posts (regardin JSON parsing issues), tried split, tried edit fields,

What is the error message (if any)?

Please share your workflow

Share the output returned by the last node

image

Any help or insights will be greatly appreciated!

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:

Can you please check the Content-Type header on the response is application/json, and not something else like text/plain?

Already tried all options…

Nothing seems to work. The output file looks OK when I open in an JSON editor, but n8n simply won’t parse it… I am not sure what I am missing…

After some trial and error testing, I noticed what needs to happen for it to work properly. I need to get rid of: \n, \ (from ") and the first and last "

image

I tried with javascript, but it looks like it is not finding matching strings and simply returns the input.

If I manually edit the input, removing \n and \

image

Then, voilà!

Has anyone ever seen anything like it?
Any ideas on how to accomplish it?

Thank you!!!

Is this URL public? It might help if we could debug this on our dev setup.

Hello @lcparreira

You can simply try to parse your string to JSON by using JSON.parse().
On your Javascript node, just give a name to your array and try to parse the content.

const myData = [{"data": YOURDATA}]

return JSON.parse(myData[0].data);

Best,
Mathias

thank you very much, I really appreciate you taking yout time to help… but this code didn’t quite work for me (tried .first() as well)

const myData = [{"data": $input.all()}]

return JSON.parse(myData[0].data);
-------------
"[object Object]" is not valid JSON [line 3]"
SyntaxError

However, once I mapped the http request output, I managed to solve with this JS:

  // load input as string
  let str = JSON.stringify($input.all());
  
  // clean input
  str = str.replaceAll('\\n','');
  str = str.replaceAll('\\r','');
  str = str.replaceAll('\\','');
  str = str.replaceAll('  ',' ');

  // http request added quotes to data array ( { "data": "[{},{}]" } )
  // so the code below removes it. Since it is always a fixed 
  // position, I indexed all quotes i string and removed these two
  let quoteIndexes = [];
  for (let i = 0; i < str.length; i++) {
    if (str[i] === '"') {
      quoteIndexes.push(i);
    }
  }

  if (quoteIndexes.length < 5) {
    console.log("Not enough quotes to remove");
    return str;
  }

  let fifthIndex = quoteIndexes[4];
  let fifthFromLastIndex = quoteIndexes[quoteIndexes.length - 5];

  //remove first quote
  let result = str.slice(0, fifthIndex) + str.slice(fifthIndex + 1);
  if (fifthFromLastIndex > fifthIndex) {
    fifthFromLastIndex -= 1;
  }
  // remove second quote
  result = result.slice(0, fifthFromLastIndex) + result.slice(fifthFromLastIndex + 1);

//console.log(result);
return JSON.parse(result);

But I still believe this is simply a jerry-rig and there’s probably a much easier way for accomplishing it in n8n…

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