# Json -\> Exec Function for Python

**URL:** <https://discourse.nodered.org/t/json-exec-function-for-python/78021>\
**Category:** General\
**Created:** [28 April 2023 19:10 UTC](https://discourse.nodered.org/t/json-exec-function-for-python/78021 "2023-04-28T19:10:00Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![krixen](https://avatars.discourse-cdn.com/v4/letter/k/e47774/32.png) [@krixen](https://discourse.nodered.org/u/krixen)\
**Post date:** [28 April 2023 19:10 UTC](https://discourse.nodered.org/t/json-exec-function-for-python/78021/1 "2023-04-28T19:10:00Z")

</div>

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:

```auto
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.

```auto
{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

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [28 April 2023 20:48 UTC](https://discourse.nodered.org/t/json-exec-function-for-python/78021/2 "2023-04-28T20:48:29Z")

</div>

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

```auto
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

 ![Untitled 4](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/f/5ffaa3311a433a8786f558b31ae9f8796c2ecc6b.jpeg)

```auto
[{"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.

---

<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:** [27 June 2023 20:49 UTC](https://discourse.nodered.org/t/json-exec-function-for-python/78021/3 "2023-06-27T20:49:00Z")

</div>

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