Erik Waters Erik Waters - 1 year ago 68
JSON Question

return JSON from python to node via spawn

I have a python script that takes two arguments; a directory and a file name.

The python script will create a JSON object from specific files in the directory provided and save it with the name being the second argument.

However if the second argument is equal to string "stream", the the JSON data is output to STDOUT.

I wrote a node script that spawns a child process to call the python script from terminal and it works as intended.

"use strict";

const spawn = require("child_process").spawn;

const command = "(path to python)";
const loc = "(path to .py script)";

const acct = process.argv[2];
const output = process.argv[3];

let callPy = spawn(command, ["", acct, output], {
cwd: loc,
stdio: "pipe"

callPy.stdout.on("data", (data) => {
if (data.toString() === "success") {
console.log(acct, "generated");
} else {


I need to pass variables to the module before the child process is spawned.

I've tried various ways of modifying the module above, so far I haven't been able to find a way to make the call successfully and be able to listen for data on the child process' STDIO.

I'm not sure if the best way to go would be a higher order function, .fork() or something else I haven't thought of.

I would prefer to not to have to use a third party module but I'm welcome to pretty much any suggestion at this point.

Answer Source

I originally thought that I should avoid spawning two separate processes for one task but it seems that the simplest solution is to fork the spawn module from the server code

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download