Search code examples
pythonnode-redpayload

How to pass json data to a python script in exec-node in node-red


I'm new to node-red, and I wish I knew how to pass json data into a python script with node exec. Here is the format of my json

{
"fftx": [
    2.6347945642924716e-17,
    0.002915638633219862,
    0.0003399789406291348,
    0.0003993145353719118,
    0.0016444053046928743,
    0.0011306178953261797,
    0.0008362894402139914,
    0.0009057875156523967,
    0.0013899906897498193,
    0.0020831589124456714,
    0.0028930687879060012,

I added a function node in order to put in the msg.payload only the msg.payload.fftx like this : node-red config

in the exec node I wrote :python3 /home/admin/script2.py msg.payload

and I try to retrieve the data in my script with this command: data=(sys.argv[2])

it works when i inject in the msg.payload a string but with a json it doesn't work anymore


Solution

  • Put a JSON node between the function node and the exec node.

    This will convert the object into a JSON string before it reaches the exec node.