# Unexpected end of JSON input in Json Node

**URL:** https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944
**Category:** General
**Created:** [11 January 2021 11:23 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944 "2021-01-11T11:23:05Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 11:23 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/1 "2021-01-11T11:23:05Z")

</div>

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

---

<div class="post-metadata">

### Author: ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)
#### Post date: [11 January 2021 11:58 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/2 "2021-01-11T11:58:48Z")

</div>

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.

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

```

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [11 January 2021 12:28 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/3 "2021-01-11T12:28:06Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [11 January 2021 12:35 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/4 "2021-01-11T12:35:07Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 12:47 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/5 "2021-01-11T12:47:12Z")

</div>

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [11 January 2021 12:48 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/6 "2021-01-11T12:48:31Z")

</div>

> [@zenofmud](#):
>
> it would seem to me you should send each one separately thru the `JSON` node

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 ?

---

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 12:48 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/7 "2021-01-11T12:48:44Z")

</div>

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

Thanks

---

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 12:55 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/8 "2021-01-11T12:55:11Z")

</div>

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

---

<div class="post-metadata">

### Author: ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)
#### Post date: [11 January 2021 13:04 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/9 "2021-01-11T13:04:30Z")

</div>

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

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

```

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [11 January 2021 13:07 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/10 "2021-01-11T13:07:55Z")

</div>

... or some additional checks

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

```

---

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 13:23 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/11 "2021-01-11T13:23:51Z")

</div>

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

---

<div class="post-metadata">

### Author: ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)
#### Post date: [11 January 2021 13:26 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/12 "2021-01-11T13:26:55Z")

</div>

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

---

<div class="post-metadata">

### Author: ![bobfield](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bobfield/32/83723_2.png) [@bobfield](https://discourse.nodered.org/u/bobfield)
#### Post date: [11 January 2021 14:09 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/13 "2021-01-11T14:09:07Z")

</div>

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

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [11 January 2021 17:16 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/14 "2021-01-11T17:16:46Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [25 January 2021 17:16 UTC](https://discourse.nodered.org/t/unexpected-end-of-json-input-in-json-node/38944/15 "2021-01-25T17:16:57Z")

</div>

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