# JSON parse error

**URL:** <https://discourse.nodered.org/t/json-parse-error/8726>\
**Category:** General\
**Created:** [7 March 2019 22:21 UTC](https://discourse.nodered.org/t/json-parse-error/8726 "2019-03-07T22:21:26Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![innotractor](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@innotractor](https://discourse.nodered.org/u/innotractor)\
**Post date:** [7 March 2019 22:21 UTC](https://discourse.nodered.org/t/json-parse-error/8726/1 "2019-03-07T22:21:26Z")

</div>

Hi,

I have written a function in order to generate a PUT towards an API, the debug node reports a JSON parse error however. Can somebody please provide me with some suggestions, the problem is related to the definition of the msg.payload:

var id = global.get('imsi');

msg.resource = id; // used as part of defining the URI in the HTTP node

msg.payload = { "Id" : id, "Timeout" : "3600" }; // something goes wrong here!!!!!????

msg.headers = {  
"Accept": "application/json",  
"Authorization": "",  
"X-DeviceNetwork": ""  
}

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:** [7 March 2019 22:33 UTC](https://discourse.nodered.org/t/json-parse-error/8726/2 "2019-03-07T22:33:26Z")

</div>

When posting code, please wrap with backticks, it makes it a lot easier for everyone.

---

<div class="post-metadata">

**Author:** ![JonG98](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jong98/32/6539_2.png) [@JonG98](https://discourse.nodered.org/u/JonG98)\
**Post date:** [8 March 2019 02:56 UTC](https://discourse.nodered.org/t/json-parse-error/8726/3 "2019-03-08T02:56:32Z")

</div>

What is 'imsi'? Is it an object, an array, etc? I am going to assume it is JSON object. How I would trouble shoot this is I would write the following furnction:

```auto
var id = global.get('imsi')
var msg.payload = id
return msg

```

That should give you a good idea of what id is coming is as. Then take that value from debug window and paste it into [try.jsonata.org](http://try.jsonata.org). From there you should be able to get your code working properly.

Try wrapping you id with {{id}} in the third command.

---

<div class="post-metadata">

**Author:** ![innotractor](https://avatars.discourse-cdn.com/v4/letter/i/90db22/32.png) [@innotractor](https://discourse.nodered.org/u/innotractor)\
**Post date:** [8 March 2019 12:20 UTC](https://discourse.nodered.org/t/json-parse-error/8726/4 "2019-03-08T12:20:50Z")

</div>

Hi,

I am quite new in this area, I will provide some additional info and backticks. The point is that I am generating a PUT request with Postman towards my API, including a body text:

PUT [https://myapi/](https://myapi/) and body text:

```auto
{
"Id" : <long id string>,
"Timeout" : 3600
}

```

This works fine. I try to perform the same activity with node-red using a function to insert the msg.headers, msg.resource and msg.payload. The API responds with "JSON parse error", see also included pics.

The problem is in defining the payload, trying to copy the exact format as the body text I used for Postman. It must be the format in some way....no clue as I am new in this area as indicated.

 ![layout](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/8/8c9140806556dede020303eb437217176768f1f2.png) ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/6/6b6cc9fcbaf8e02610c0a38ac4eb011d9c58929f.png)

Function code:

```auto
var id = global.get('imsi'); // defining variable id to be used as part of URI (see pic)
msg.resource = id;

msg.payload = { "Id" : id, "Timeout" : "3600" }; // something goes wrong here, format?

msg.headers = { // standard header, I took out the Ids for safety reasons
"Accept": "application/json",
"Authorization": "",
"X-DeviceNetwork": ""
}

return msg;

```

---

<div class="post-metadata">

**Author:** ![bakman2](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/bakman2/32/6207_2.png) [@bakman2](https://discourse.nodered.org/u/bakman2)\
**Post date:** [9 March 2019 07:21 UTC](https://discourse.nodered.org/t/json-parse-error/8726/5 "2019-03-09T07:21:01Z")

</div>

[Where are the backticks](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) ?

---

<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:** [9 March 2019 09:14 UTC](https://discourse.nodered.org/t/json-parse-error/8726/6 "2019-03-09T09:14:32Z")

</div>

See the link below for more information, but in brief, if you start a new line then click on the icon \</\> above the edit box it will put the backticks in for you.

> [@How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506):
>
> To share code samples or flow json in this forum, you need to take care to format it properly so that it is displayed correctly. The easiest way to do that is to click the 'Preformatted Text' button in the toolbar: [image] Then paste in your code or flow json. You should end up with three back-tick characters - ``` - on their own line before and after the code. On standard US English keyboards, this is on the same key as the ~ character. Using the backticks will ens…

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [9 March 2019 09:19 UTC](https://discourse.nodered.org/t/json-parse-error/8726/7 "2019-03-09T09:19:15Z")

</div>

At a glance, the Function code looks fine - assuming `imsi` is already defined in global context to a valid value.

Wire in a Debug node after the Function node so you can see the actual payload it is generating
