# Help Parsing HTTP payload information

**URL:** https://discourse.nodered.org/t/help-parsing-http-payload-information/16436
**Category:** General
**Created:** [7 October 2019 14:48 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436 "2019-10-07T14:48:15Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [7 October 2019 14:48 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/1 "2019-10-07T14:48:15Z")

</div>

I'm using an HTTP API (OctoPrint Enclosure plugin for those that care) that returns the following payload:

`payload: "[{"status": false, "index_id": 3}, {"status": false, "index_id": 4}, {"status": false, "index_id": 5}, {"status": true, "index_id": 6}]"`

I would like to take each status and use it to sync my input booleans in Home Assistant. Can someone help me with a node that would parse out the true/false for each index id?

Thank you!

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [7 October 2019 14:56 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/2 "2019-10-07T14:56:23Z")

</div>

If you look at the surrounding quotes, you can see that the payload is a stringified JSON array. Step one is to feed the payload to the builtin JSON node to turn it into a usable object. Next, you can use the builtin `split` node to handle each item in the array. See [working with messages](https://nodered.org/docs/user-guide/messages) and read the part about message sequences too.

---

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [7 October 2019 15:09 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/3 "2019-10-07T15:09:45Z")

</div>

That is very helpful. It gets me four objects, with the following payload format:

![58%20AM](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/a/aa5e144a58df98f756af5de0cf288f16089da405.png)

How do I convert this to something useable, i.e. index 5 false means input\_boolean X is "off"

---

<div class="post-metadata">

### Author: ![burtonboucher](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/burtonboucher/32/7806_2.png) [@burtonboucher](https://discourse.nodered.org/u/burtonboucher)
#### Post date: [7 October 2019 15:29 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/4 "2019-10-07T15:29:28Z")

</div>

One way is to use a function node and write javascript. You can access the msg.payload object directly. Need to be a bit more specific on your output.

get value of index\_id: X=msg.payload.index\_id // number  
get value of status: status=msg.payload.status // bool

example Check specifically status is false and index\_id is 5  
var mystring;  
if (!msg.payload.status && msg.payload.index\_id ==5){  
mystring = "input\_boolean " + msg.payload.index\_id + " is off";  
} else {  
mystring = "input\_boolean " + msg.payload.index\_id + " is on";  
}

msg.payload=mystring;

---

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [7 October 2019 16:44 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/5 "2019-10-07T16:44:53Z")

</div>

Could you help me figure out the JS to make a separate output with payload on/off if status=true (or false) and index\_id=5?

Also, the same for index 3,4, and 6 🙂

The following does not seem to be working or doing anything:

```auto
if (msg.payload[2].status ===false && msg.payload[2].index_id ==5)
{
mystring = "input_boolean " + msg.payload[2].index_id + " is off";
} else {
imystring = "input_boolean " + msg.payload[2].index_id + " is on";
}

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: [7 October 2019 17:47 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/6 "2019-10-07T17:47:53Z")

</div>

You could use something like this (works for any id and status):

```auto
index = msg.payload.index_id
status = msg.payload.false
strStatus = (status) ? "on" : "off" 
out = "input_boolean " + index + " " + strStatus 

return {payload:out};

```

---

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [7 October 2019 18:10 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/7 "2019-10-07T18:10:43Z")

</div>

Feeding this from the split node, I get the following result in the debug node:

> payload: "input\_boolean 5 off"

Regardless of the actual status (true/false) coming form the html call. All four objects are returning false values.

---

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [7 October 2019 18:23 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/8 "2019-10-07T18:23:39Z")

</div>

I think I got it working...thank you @bakman2! Was a slight typo in your response. Current working code:

```auto
index = msg.payload.index_id;
status = msg.payload.status;
strStatus = (status) ? "on" : "off" ;
out = "input_boolean " + index + " " + strStatus; 

return {payload:out};

```

One other add-on question:

If the payload is now "RelayX on" or RelayX off", what node would I use to get the payload to simply be on or off?

---

<div class="post-metadata">

### Author: ![afelix](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/afelix/32/9743_2.png) [@afelix](https://discourse.nodered.org/u/afelix)
#### Post date: [7 October 2019 18:49 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/9 "2019-10-07T18:49:56Z")

</div>

> [@Exit2Studios](#):
>
> If the payload is now "RelayX on" or RelayX off", what node would I use to get the payload to simply be on or off?

On/off as values: the boolean like it is now (true/false in status)  
On/off as text: the strings like you have them now in your function

---

<div class="post-metadata">

### Author: ![burtonboucher](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/burtonboucher/32/7806_2.png) [@burtonboucher](https://discourse.nodered.org/u/burtonboucher)
#### Post date: [8 October 2019 01:48 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/10 "2019-10-08T01:48:54Z")

</div>

This splits the array into one msg per item. Then switch used to direct each msg to specific output (0-6). Then 6 is split further into on / off (true/false). from there you can set any string value. Lot more nodes this way but you should be able to accomplish. Suggest mapping out the the exact flow first, even if on paper. Like doing a math problem showing every operational step. Then think about existing node or nodes can accomplish the task.

```auto
[{"id":"21f3a38.97fed5c","type":"switch","z":"2d36d476.ac4cec","name":"","property":"payload.index_id","propertyType":"msg","rules":[{"t":"eq","v":"0","vt":"num"},{"t":"eq","v":"1","vt":"num"},{"t":"eq","v":"2","vt":"num"},{"t":"eq","v":"3","vt":"num"},{"t":"eq","v":"4","vt":"num"},{"t":"eq","v":"5","vt":"num"},{"t":"eq","v":"6","vt":"num"}],"checkall":"true","repair":false,"outputs":7,"x":890,"y":460,"wires":[["e58a22de.52f1a"],["fde85767.147a38"],["85af23a7.d2c9"],["e2091775.3a8ab8"],["4c114ee7.141d9"],["6601b1b.71b545"],["b6ff39a4.a4f8c8","e8872f4.17ecdd"]]},{"id":"1682af87.84b27","type":"function","z":"2d36d476.ac4cec","name":"simulate OctoPrint","func":"msg.payload=[{\"status\": false, \"index_id\": 3}, {\"status\": false, \"index_id\": 4}, {\"status\": false, \"index_id\": 5}, {\"status\": false, \"index_id\": 6}];\nreturn msg;","outputs":1,"noerr":0,"x":530,"y":460,"wires":[["202b2836.aa9a98","cda33154.483cb"]]},{"id":"19615fac.c38bf","type":"inject","z":"2d36d476.ac4cec","name":"","topic":"","payload":"button","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":330,"y":460,"wires":[["1682af87.84b27"]]},{"id":"e58a22de.52f1a","type":"debug","z":"2d36d476.ac4cec","name":"0","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":360,"wires":[]},{"id":"fde85767.147a38","type":"debug","z":"2d36d476.ac4cec","name":"1","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":400,"wires":[]},{"id":"85af23a7.d2c9","type":"debug","z":"2d36d476.ac4cec","name":"2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":440,"wires":[]},{"id":"e2091775.3a8ab8","type":"debug","z":"2d36d476.ac4cec","name":"3","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":480,"wires":[]},{"id":"4c114ee7.141d9","type":"debug","z":"2d36d476.ac4cec","name":"4","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":520,"wires":[]},{"id":"6601b1b.71b545","type":"debug","z":"2d36d476.ac4cec","name":"5","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":560,"wires":[]},{"id":"202b2836.aa9a98","type":"debug","z":"2d36d476.ac4cec","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1070,"y":240,"wires":[]},{"id":"cda33154.483cb","type":"split","z":"2d36d476.ac4cec","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":750,"y":460,"wires":[["21f3a38.97fed5c","68084b03.a8a284"]]},{"id":"68084b03.a8a284","type":"debug","z":"2d36d476.ac4cec","name":"","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1070,"y":280,"wires":[]},{"id":"b6ff39a4.a4f8c8","type":"debug","z":"2d36d476.ac4cec","name":"6","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1070,"y":600,"wires":[]},{"id":"e8872f4.17ecdd","type":"switch","z":"2d36d476.ac4cec","name":"seperate status 6","property":"payload.status","propertyType":"msg","rules":[{"t":"eq","v":"true","vt":"jsonata"},{"t":"eq","v":"false","vt":"jsonata"}],"checkall":"true","repair":false,"outputs":2,"x":1120,"y":660,"wires":[["420eb42b.f9f43c"],["68c91688.c4f228"]],"outputLabels":["On","Off"]},{"id":"420eb42b.f9f43c","type":"debug","z":"2d36d476.ac4cec","name":"on","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1330,"y":620,"wires":[]},{"id":"68c91688.c4f228","type":"debug","z":"2d36d476.ac4cec","name":"off","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":1330,"y":680,"wires":[]}]

```

---

<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: [8 October 2019 05:46 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/11 "2019-10-08T05:46:46Z")

</div>

Please export your flow again, [read this post](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506) and edit your post.

---

<div class="post-metadata">

### Author: ![Exit2Studios](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/exit2studios/32/10998_2.png) [@Exit2Studios](https://discourse.nodered.org/u/Exit2Studios)
#### Post date: [8 October 2019 18:55 UTC](https://discourse.nodered.org/t/help-parsing-http-payload-information/16436/12 "2019-10-08T18:55:36Z")

</div>

Cool thing about NR...a lot of ways to skin the same cat. Here is what "I" came up with (with the help of this post)...very similar:

```auto
[{"id":"b9cb942f.daadd8","type":"tab","label":"Clipboard","disabled":false,"info":""},{"id":"62a0da9c.76e4dc","type":"http request","z":"b9cb942f.daadd8","name":"Status","method":"GET","ret":"txt","paytoqs":false,"url":"http://xxx.xxx.x.x/plugin/enclosure/getOutputStatus?apikey=xxxxxxx","tls":"","proxy":"","authType":"","x":210,"y":220,"wires":[["1192b25d.cba726"]]},{"id":"1192b25d.cba726","type":"json","z":"b9cb942f.daadd8","name":"","property":"payload","action":"","pretty":false,"x":330,"y":220,"wires":[["bbafd4f1.4f21e"]]},{"id":"dda1b26a.658678","type":"inject","z":"b9cb942f.daadd8","name":"","topic":"","payload":"","payloadType":"date","repeat":"15","crontab":"","once":true,"onceDelay":0.1,"x":250,"y":260,"wires":[["62a0da9c.76e4dc"]]},{"id":"bbafd4f1.4f21e","type":"split","z":"b9cb942f.daadd8","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"","x":450,"y":220,"wires":[["d6cbecb5.8b7ee8"]]},{"id":"d6cbecb5.8b7ee8","type":"function","z":"b9cb942f.daadd8","name":"on/off","func":"index = msg.payload.index_id;\nstatus = msg.payload.status;\nstrStatus = (status) ? \"on\" : \"off\" ;\nout = \"Relay\" + index + \" \" + strStatus; \n\nreturn {payload:out};","outputs":1,"noerr":0,"x":570,"y":220,"wires":[["701d86b.1b547f8"]]},{"id":"701d86b.1b547f8","type":"switch","z":"b9cb942f.daadd8","name":"","property":"payload","propertyType":"msg","rules":[{"t":"cont","v":"Relay3","vt":"str"},{"t":"cont","v":"Relay4","vt":"str"},{"t":"cont","v":"Relay5","vt":"str"},{"t":"cont","v":"Relay6","vt":"str"}],"checkall":"true","repair":false,"outputs":4,"x":690,"y":220,"wires":[["cc0cce0f.1c37f8"],["81d404e3.f5dbd8"],["2448b2ff.bf5dbe"],["311be413.718134"]]},{"id":"cc0cce0f.1c37f8","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":147,"wires":[["8c7eafd3.258f3"]]},{"id":"81d404e3.f5dbd8","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":194,"wires":[["6997b34a.7f7f1c"]]},{"id":"2448b2ff.bf5dbe","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":237,"wires":[["558a99c8.9f631"]]},{"id":"311be413.718134","type":"string","z":"b9cb942f.daadd8","name":"on/off","methods":[{"name":"getRightMost","params":[{"type":"str","value":" "}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":840,"y":282,"wires":[["4539a0e3.411b48"]]},{"id":"8c7eafd3.258f3","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":147,"wires":[["8c3a2ed7.fbfde"]]},{"id":"6997b34a.7f7f1c","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":194,"wires":[["d640e286.bc71b8"]]},{"id":"558a99c8.9f631","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":238,"wires":[["5517309d.7fd968"]]},{"id":"4539a0e3.411b48","type":"rbe","z":"b9cb942f.daadd8","name":"","func":"rbe","gap":"","start":"","inout":"out","property":"payload","x":950,"y":282,"wires":[["dddfd9f8.c90f38"]]},{"id":"8c3a2ed7.fbfde","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_power","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":147,"wires":[[]]},{"id":"d640e286.bc71b8","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_lights","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":194,"wires":[[]]},{"id":"5517309d.7fd968","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_dryer","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":239,"wires":[[]]},{"id":"dddfd9f8.c90f38","type":"api-call-service","z":"b9cb942f.daadd8","name":"HA Update","server":"6c32e276.37c47c","version":1,"service_domain":"homeassistant","service":"turn_{{payload}}","entityId":"input_boolean.printer_relay","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1081,"y":284,"wires":[[]]},{"id":"6c32e276.37c47c","type":"server","z":"","name":"HOME ASSISTANT","legacy":false,"hassio":true,"rejectUnauthorizedCerts":true,"ha_boolean":"","connectionDelay":true}]

```
