Unexpected end of JSON input in Json Node

Dears,

I'm appending a Json records sequence in a text file using the 'File Node', and the last line is a newline character. After, I read this file with 'File In Node' and 'Json Node' to convert in Json. It works, but always return the error message 'Unexpected end of JSON input in Json Node' when the node reads the newline. Is there some solution to avoid this error?

Thanks

I think that if you feed it through a function node containing this (which trims white space characters off the ends of the string) then it should be ok. Newlines count as white space characters. Feed it into the JSON node after the function node.

msg.payload = msg.payload.trim()
return msg

I wouldn't expect the json node to trip over white-space. as far as I know, white-space is valid in json.

1 Like

Is the file-in node set to read a single utf8 string or a msg per line?

Since each line of the file contains a JSON string, it would seem to me you should send each one separately thru the JSON node.

Hi Colin,
I did your recommendation, but doesn't work. The error was returned again.
Thanks

i agree .. msg per line .. and maybe a function node .. after the File in node that filters any \n or \r or "" empty lines before sending it to the Json node.

@bobfield Would it possible to post the text file ?

Hi zenofmud,
The node is set do read a msg per line.

Thanks

Hi UnborN, I tried to send the file, but received a message said me that new users can't to do upload files. Sorry,.

add this in a function node just before the json node.

if(msg.payload.trim()===""){
    return;
}
return msg;

Or return the complete file trim it and then split it with split node.

... or some additional checks

if(msg.payload == "\n" || msg.payload == "\r" || msg.payload == "" ){
    return null;
}
else {
return msg;
}

Thank you very much Unborn,

It works. I follows your recommenadtion, and added the 'node-red-contrib-filter' to my pallet. Then, before the Json Node, I filter the content file to records != (string with no filling).

Best Regards,
Roberto

you didnt have to install a node for that
i meant the code in the Function node would act as a "filter" :wink:

Oh, Excelent! I will do this. Sorry, but I didn't see the message with the code. Thanks

No need for a function node.

A very easy way to get rid of the last line being read in is to add a switch node after the file is and use the 'is not empty' test. That way the last line will not pass on, but the others will.

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