# Payload Sub-String to JSON

**URL:** <https://discourse.nodered.org/t/payload-sub-string-to-json/40994>\
**Category:** General\
**Created:** [15 February 2021 06:04 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994 "2021-02-15T06:04:08Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![TutanJamon](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@TutanJamon](https://discourse.nodered.org/u/TutanJamon)\
**Post date:** [15 February 2021 06:04 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/1 "2021-02-15T06:04:08Z")

</div>

Good morning and excuse my English. This is my second participation in the forum. I am trying to convert the response of a python file into JSON, but the payload contains other strings besides the one I want to convert. The strings "before" and "after" should be ignored. My goal is to get a JSON that I can then somehow display on the dashboard. Thanks a lot.

```auto
[{"id":"f85aca3.1724b38","type":"exec","z":"ce7d7a5e.e05188","command":"python3 /home/pi/Desktop/return.py","addpay":false,"append":"","useSpawn":"false","timer":"","oldrc":false,"name":"return.py","x":980,"y":580,"wires":[["c8d0c1df.9d9e2","588ca8df.9cc898"],["65c03a65.1774e4"],["2abbcd10.2c0472"]]},{"id":"779faa97.8d70d4","type":"inject","z":"ce7d7a5e.e05188","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":840,"y":580,"wires":[["f85aca3.1724b38"]]},{"id":"c8d0c1df.9d9e2","type":"debug","z":"ce7d7a5e.e05188","name":"out","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1150,"y":540,"wires":[]},{"id":"65c03a65.1774e4","type":"debug","z":"ce7d7a5e.e05188","name":"error","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":580,"wires":[]},{"id":"2abbcd10.2c0472","type":"debug","z":"ce7d7a5e.e05188","name":"return","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1150,"y":620,"wires":[]},{"id":"588ca8df.9cc898","type":"json","z":"ce7d7a5e.e05188","name":"","property":"payload","action":"","pretty":false,"x":1150,"y":500,"wires":[["b5d23173.f80f8"]]},{"id":"b5d23173.f80f8","type":"debug","z":"ce7d7a5e.e05188","name":"out json","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1300,"y":500,"wires":[]}]

```

Nodes:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/7/1/71649b16d7572c675b8117636e317a60920efb66.png)

My python file:  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/e/aec3a55e171994f52d1f7d1410babbd89c67b866.png)

Output debug:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/1/815a154c2003c1b48b047fd527ba021086ac6622.png)

---

<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:** [15 February 2021 06:48 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/2 "2021-02-15T06:48:33Z")

</div>

Hi i would try removing `print("antes")` and `Print(despues")` from the Python file, then the output would be a properly formated json string. You would then just feed it through the json node.

---

<div class="post-metadata">

**Author:** ![TutanJamon](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@TutanJamon](https://discourse.nodered.org/u/TutanJamon)\
**Post date:** [15 February 2021 06:58 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/3 "2021-02-15T06:58:07Z")

</div>

I understand this, but I can't do it because the user must keep getting some messages from the system console. 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:** [15 February 2021 07:30 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/4 "2021-02-15T07:30:16Z")

</div>

Use a command line flag to say whether it is being run manually by a user, so the console messages are shown, or via node red, in which case it sends as JSON.

---

<div class="post-metadata">

**Author:** ![TutanJamon](https://avatars.discourse-cdn.com/v4/letter/t/d2c977/32.png) [@TutanJamon](https://discourse.nodered.org/u/TutanJamon)\
**Post date:** [15 February 2021 07:32 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/5 "2021-02-15T07:32:55Z")

</div>

The problem is that the "payload" collects all the prints that I run in python, whether or not they are JSON. I don't understand how to solve this with your answer, thank you.

---

<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:** [15 February 2021 07:33 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/6 "2021-02-15T07:33:28Z")

</div>

Then just remove the words "andes" and "despues" from the string.  
This could be done in a change node or function node.

change node-  
change msg.payload -search regex for `"^antes\s*|\s*despues\s*$"` replace with "".

function node -  
`msg.payload = msg.payload.replace(/^antes\s*|\s*despues\s*$/g, "")`

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [15 February 2021 08:11 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/7 "2021-02-15T08:11:12Z")

</div>

Instead of sending these random values as plain strings, send them as JSON then use a switch node to differentiate the messages. Better still, include a property like topic to aid differentiation.

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [15 February 2021 08:13 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/8 "2021-02-15T08:13:34Z")

</div>

Or send the random strings to stderr instead

```auto
print("your message", file=sys.stderr)

```

Then they will appear on the second pin of the exec node, but look ok on console

---

<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:** [16 April 2021 08:13 UTC](https://discourse.nodered.org/t/payload-sub-string-to-json/40994/9 "2021-04-16T08:13:36Z")

</div>

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