Payload Sub-String to JSON

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.

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

My python file:
image

Output debug:

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.

I understand this, but I can't do it because the user must keep getting some messages from the system console. Thanks.

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.

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.

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, "")

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.

Or send the random strings to stderr instead

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

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

2 Likes