Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

194
Visualizações
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 à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda