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

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 :heart:.

The flow JSON:

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

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("}")

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

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

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

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

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

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