# Strange behavior of my simple API

**URL:** <https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289>\
**Category:** General\
**Created:** [18 March 2020 20:06 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289 "2020-03-18T20:06:26Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 March 2020 20:06 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/1 "2020-03-18T20:06:27Z")

</div>

I'm trying a simple API, and I have a strange behavior with the result.  
The result is JSON formatted, the data come from a DB query.  
The result has unwanted escape characters:

**"{"type":[{"sensor":"temperature", "unit":"celsius"}, {"sensor":"humidity", "unt":"relative"}]}"**

To be sure that this isn't due to the DB response, I've modified the payload. Now it's static.  
So, i'm sure, the msg.payload, passed to http request, is without escape characters:  
**{"type":[{"sensor":"temperature", "unit":"celsius"}, {"sensor":"humidity", "unt":"relative"}]}**  
checked with debug node, but te response from http request is still with escape characters.  
How to remove them?

Thanks, Fabrizio

---

<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:** [18 March 2020 20:54 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/2 "2020-03-18T20:54:45Z")

</div>

Do you mean the double quotes round the outside? Can you feed the response into a debug node and screenshot what you see and paste it here so we can fully understand what you mean please.

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 March 2020 21:56 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/3 "2020-03-18T21:56:04Z")

</div>

This is te payload of http request:

 ![Query](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/b/9b0927925da014f40003804338df7db8df0824e6.png)

and this is the result:

 ![result](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/c/bcb4ef366e15629756fe6e95dd6cf57668ecf543.png)

The result adds \ as escape character.

And also the headres of the http response:

 ![headers](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/1/01201a8b5a61f87c228603baf6d7d4ac4ee397e8.png)

---

<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:** [18 March 2020 22:22 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/4 "2020-03-18T22:22:37Z")

</div>

This happens because your property `msg.payload.d_type` is a JSON encoded string. That means it stays as a String when `msg.payload` is itself JSON encoded and returned.

Add a JSON node and configure it to parse `msg.payload.d_type` to an object before the HTTP Response node.

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 March 2020 23:16 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/5 "2020-03-18T23:16:57Z")

</div>

Thanks knolleary for your suggestion.

I had already done it, but without results.

Here the modified flow with the added JSON node.

 ![Query1](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/a/2ab7bd1d8e558f52f04e7b5d15388a6ee65d38ed.png)

Even changing the action, I have the same result. 🙄

---

<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:** [18 March 2020 23:19 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/6 "2020-03-18T23:19:26Z")

</div>

In your original screenshot, `msg.payload` was an object with a property called `d_type` that needed parsing.

In this most recent screenshot, `msg.payload` is an array of objects. That means the path to the element you need to convert is `msg.payload[0].d_type`.

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [18 March 2020 23:30 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/7 "2020-03-18T23:30:11Z")

</div>

Ok, sorry, my mistake. I've forgotten the array index. payload[0].d\_type  
Now a new question: how to apply to all elements of the array?  
Thanks.

---

<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:** [18 March 2020 23:32 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/8 "2020-03-18T23:32:58Z")

</div>

One option would be to add a Split node before the JSON node and a Join node afterwards. Use their default configuration and change the JSON node back to `msg.payload.d_type`.

Another option would be to replace the JSON node with a Function node:

```auto
msg.payload = msg.payload.map(function(v) {
   v.d_type = JSON.parse(v.d_type);
   return v;
});
return msg;

```

---

<div class="post-metadata">

**Author:** ![rex](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/rex/32/59848_2.png) [@rex](https://discourse.nodered.org/u/rex)\
**Post date:** [19 March 2020 09:33 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/9 "2020-03-19T09:33:11Z")

</div>

> [@knolleary](#):
>
> node afterwards. Use their

I've adopted the second solution. I'm a bungler, so it has taken me more time than really need, but it works.  
Thanks,  
Fabrizio

---

<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:** [2 April 2020 09:33 UTC](https://discourse.nodered.org/t/strange-behavior-of-my-simple-api/23289/10 "2020-04-02T09:33:18Z")

</div>

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