JSON parse error

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;

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

1 Like

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:

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. From there you should be able to get your code working properly.

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

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/ and body text:

{
"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.

image

Function code:

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;

Where are the backticks ?

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.

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