# Problem with: Template (JSON) \> Exec (Python) \> Payload (Output)

**URL:** <https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644>\
**Category:** General\
**Tags:** function-node\
**Created:** [15 November 2022 12:11 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644 "2022-11-15T12:11:57Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![steveneyre](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@steveneyre](https://discourse.nodered.org/u/steveneyre)\
**Post date:** [15 November 2022 12:11 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/1 "2022-11-15T12:11:57Z")

</div>

I'm having some trouble injecting JSON into a Python script being executed using the Exec node. The JSON is held in a template node. I've read all the other forum posts on this and similar but I am still hitting a dead end. The Flow JSON and (very simple) Python script are below.

Any help to resolve this would be most gratefully received ❤.

The flow JSON:

```auto
[
    {
        "id": "7f8485d6fdbb1016",
        "type": "tab",
        "label": "Syspro Business Objects",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "72ba41ac93f463b6",
        "type": "inject",
        "z": "7f8485d6fdbb1016",
        "name": "Inject",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 170,
        "y": 160,
        "wires": [
            [
                "432ef353974e0f43"
            ]
        ]
    },
    {
        "id": "3dc73cd34e6c9e81",
        "type": "debug",
        "z": "7f8485d6fdbb1016",
        "name": "",
        "active": true,
        "tosidebar": true,
        "console": false,
        "tostatus": false,
        "complete": "payload",
        "targetType": "msg",
        "statusVal": "",
        "statusType": "auto",
        "x": 770,
        "y": 160,
        "wires": []
    },
    {
        "id": "7c06188252ebc2f3",
        "type": "exec",
        "z": "7f8485d6fdbb1016",
        "command": "python D:\\Python\\Syspro\\SORQRY.py ",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "SORQRY",
        "x": 580,
        "y": 160,
        "wires": [
            [
                "3dc73cd34e6c9e81"
            ],
            [
                "3dc73cd34e6c9e81"
            ],
            [
                "3dc73cd34e6c9e81"
            ]
        ]
    },
    {
        "id": "432ef353974e0f43",
        "type": "template",
        "z": "7f8485d6fdbb1016",
        "name": "SORQRY_params",
        "field": "payload",
        "fieldType": "msg",
        "format": "json",
        "syntax": "mustache",
        "template": "{\n \"Key\":{\n \"SalesOrder\":\"000231\"\n },\n \"Options\":{\n \"DatabaseLetter\":\"T\",\n \"OutputFormat\":\"JSON\",\n \"IncludeStockedLines\":\"Y\",\n \"IncludeNonStockedLines\":\"Y\",\n \"IncludeFreightLines\":\"Y\",\n \"IncludeMiscLines\":\"Y\",\n \"IncludeCommentLines\":\"Y\",\n \"IncludeCompletedLines\":\"Y\",\n \"IncludeSerials\":\"N\",\n \"IncludeLots\":\"Y\",\n \"IncludeBins\":\"Y\",\n \"IncludeAttachedItems\":\"N\",\n \"IncludeCustomForms\":\"Y\",\n \"IncludeDetailLineCustomForms\":\"Y\",\n \"IncludeValues\":\"N\",\n \"ReturnLineShipDate\":\"N\"\n }\n}",
        "output": "json",
        "x": 370,
        "y": 160,
        "wires": [
            [
                "7c06188252ebc2f3"
            ]
        ]
    }
]

```

The Python script:

```auto
import json
import sys

# read json payload from first commend line parameter
json_payload = sys.argv[1:]
data_dict = json_payload.json()

key_SalesOrder = data_dict["Key"]["SalesOrder"]

option_DatabaseLetter = data_dict["Options"]["DatabaseLetter"]
option_OutputFormat = data_dict["Options"]["OutputFormat"]

print("{")
print(" 'Key': '" + key_SalesOrder + "'," )
print(" 'DatabaseLetter': '" + option_DatabaseLetter + "'" )
print("}")

```

---

<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 November 2022 22:09 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/3 "2022-11-15T22:09:11Z")

</div>

What do you see in msg.payload if you feed that into a debug node?

---

<div class="post-metadata">

**Author:** ![steveneyre](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@steveneyre](https://discourse.nodered.org/u/steveneyre)\
**Post date:** [16 November 2022 11:30 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/4 "2022-11-16T11:30:53Z")

</div>

If I add a debug node to the output of the Template then I get the full JSON held in the Template node. If the setting is Output as 'Plain TExt' then I get a string and if set to 'Formatted JSON' I get a JSON array out

---

<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:** [16 November 2022 21:25 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/5 "2022-11-16T21:25:02Z")

</div>

What command, entered in a command window, are you trying to replicate in the exec node? Something like  
`python D:\Python\Syspro\SORQRY.py param1 param2 ...`

---

<div class="post-metadata">

**Author:** ![steveneyre](https://avatars.discourse-cdn.com/v4/letter/s/b5a626/32.png) [@steveneyre](https://discourse.nodered.org/u/steveneyre)\
**Post date:** [16 November 2022 21:37 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/6 "2022-11-16T21:37:05Z")

</div>

Hi Colin, yes that's exactly what I'm trying to do, pass the JSON payload in as param1 (in your example)

---

<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:** [16 November 2022 21:49 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/7 "2022-11-16T21:49:20Z")

</div>

Please show us an exact example command that works in the command line but not in node-red

---

<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:** [15 January 2023 21:50 UTC](https://discourse.nodered.org/t/problem-with-template-json-exec-python-payload-output/70644/8 "2023-01-15T21:50:00Z")

</div>

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