Json -> Exec Function for Python

I have seen many topics in here about getting a json input into an exec call for python but a lot of the posts end before the answer is ever found.

I have several python calls I intend to make in the node red dashboard, that will take in static JSON objects. To do this, I created a "TEMPLATE" node, which houses my json data.

In order to get the data prepped for the exec call, I have the TEMPLATE format set to "Plain Text", and output as "Parsed JSON". I then send this to a JSON node. Lastly, this is sent to my exec call.

I append the msg.payload to the exec call and printing it as:

import sys
print(sys.argv[1])

The issue is, sys.argv will strip the quotations off of my json object, so I cannot load it as json. I saw in another post someone mentioning using ARGPARSER instead, however, I do not know how to add an arguement using the EXEC node in node-red.

Here is an output from my python script where you can see "controls" has no quotations and so on.

{controls:{Sample1:{flowrate:10,direction:aspirate,valveposition:output}},stopcondition:User}

So my questions are:

  1. Is there a way to read in JSON in a python script from the exec node with the quotations intact without using an argparser?

  2. If "1" is not possible, how can I use argparser with the exec node. i.e. how do I add named parameters to the exec node

  1. Good question. There certainly ought to be.
  2. The example using argparse which someone mentioned is intended to handle arguments passed as a JSON string, so it's a fair bet that it can handle more than one data element within a JSON string.

Here is a python script try.py

import argparse
import json

# Set up the argument parser
parser = argparse.ArgumentParser()
parser.add_argument('JSON', metavar='json',type=str, help='JSON to parse')

args = parser.parse_args()

# Get JSON data
json_str = args.JSON

# Parse JSON data
json_data = json.loads(json_str)

print(json_data["name"])
print(json_data["age"])

Here is a simple flow

[{"id":"0e8ff8e87d0fdf7d","type":"inject","z":"aefbc0d924a8ddfd","name":"Parameter JSON as string","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"name\":\"Bill\", \"age\":42}","payloadType":"str","x":150,"y":60,"wires":[["4f2b8f306542ca0c"]]},{"id":"f3a0d09e7688f675","type":"exec","z":"aefbc0d924a8ddfd","command":"python /home/pi/try.py ","addpay":"payload","append":"","useSpawn":"false","timer":"","winHide":false,"oldrc":false,"name":"","x":580,"y":60,"wires":[["2a4b7edae62b0713"],["2a4b7edae62b0713"],[]]},{"id":"2a4b7edae62b0713","type":"debug","z":"aefbc0d924a8ddfd","name":"debug 294","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":60,"wires":[]},{"id":"4f2b8f306542ca0c","type":"change","z":"aefbc0d924a8ddfd","name":"Wrap it in 'quotes'","rules":[{"t":"set","p":"payload","pt":"msg","to":"\"'\" & payload & \"'\"","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":370,"y":60,"wires":[["f3a0d09e7688f675"]]}]

It works for me (on Linux); I read the other post and did a couple of experiments.
Please see if it works for you and let us know.

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