Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

195
Vistas
how to return a json object from a javascript function which runs a python scrip?

I am trying to return a json object from a javascript function which runs a python script.

I have a python script which goal is to build a list as below (here i provide only the result).

mydict ={
  "counterParty_Car": "Ford",
  "counterParty_CarPlate" : "O XXX YYY",
  "counterParty_Name" : "John Snow",
  "counterParty_Policy": "0123456789"      
}
print(mydict)

this python script is launched by a function on javascript using pyhton-shell

function runPythonScript(pythonPath, pythonScript){
    console.log('ND - server - run python without argument');
    let myPath = __dirname;
    console.log('myPath: ', myPath);
    //let pythonPath = pythonPath;
    console.log('pythonPath: ', pythonPath);
    //let pythonScript = pythonScript;
    console.log('pythonScript: ', pythonScript);
    
    //Here are the option object in which arguments can be passed for the python_test.js.
    let options = {
        mode: 'text',
        pythonOptions: ['-u'], // get print results in real-time
        scriptPath: myPath + pythonPath //If you are having python_test.py script in same folder, then it's optional.
        //args: ['xxxx'] //An argument which can be accessed in the script using sys.argv[1]
    };

    PythonShell.run(pythonScript, options, function (err, result){
        if (err) throw err;
        // result is an array consisting of messages collected
        //during execution of script.
        console.log('data: ', result.toString());
        let parsedData = JSON.parse(JSON.stringify(result.join('\n')));
        console.log('data type: ',typeof parsedData, ' of length ', parsedData.length);
        parsedData = parsedData.replace(/'/g, `"`); // replace the quote by the double quotes for parsing the string to a JSON object.
        parsedData = JSON.parse(parsedData);
        console.log('parsed data type: ', typeof parsedData);
        console.log('parsed data: ', parsedData);
        return parsedData;
    });
}

In javascript I finally call the runPythonScript as below

let counterpartyData = runPythonScript('/python','test.py');
console.log('ND - server - counterparty data: ', counterpartyData);

But when check the result on the console, i see that counterpartyData is undefined. Do you have an idea why?

ND - server - run python without argument
myPath:  /app
pythonPath:  /python
pythonScript:  test.py
[...]
parsed data:  {
counterParty_Car: 'Ford',
counterParty_CarPlate: 'O XXX YYY',
counterParty_Name: 'John Snow',
counterParty_Policy: '0123456789'
}
ND - server - counterparty data:  undefined
about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda