Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

212
Views
Parse the result of a python call with Node.js

I am trying to parse the result of my python call with JSON.parse . I provide here only the result of my python script (the dictionary).

python script

import json 

mydict =    {
  "brand": "Ford",
  "model": "Mustang",
  "licensePlate" : "O XXX YYY",
  "year" : 1964,
  "insured": {
                'lastname' : 'Snow', 
                'firstname' : 'John', 
                'adress' : 'Holiday street, 4 - Paradise'
             }
}
myjsonlist = json.dumps(mydict, indent = 4) 

print(myjsonlist) 

my javascript calls the python script and tries to parse the result(= the dictionary from python). The goal is to be able to use this dictionary in javascript. In other words, I would like to extract in javascript the brand of the car like brandCar = resut.brand .

//Import express.js module and create its variable.
const express=require('express');
const app=express();

//Import PythonShell module.
const {PythonShell} =require('python-shell');

//Router to handle the incoming request.

//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: '/mnt/c/Users/xxxxx/javascript/' //If you are having python 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.py', options, function (err, result){
    if (err) throw err;
    // result is an array consisting of messages collected
    //during execution of script.
    console.log('result: ', result.toString());

    console.log(JSON.parse(result[0]));
});

In the console, i have this as error message and result

result:  {,    "brand": "Ford",,    "model": "Mustang",,    "licensePlate": "O XXX YYY",,    "year": 1964,,    "insured": {,        "lastname": "Snow",,        "firstname": "John",,        "adress": "Holiday street, 4 - Paradise",    },}
undefined:1
{

SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)
    at /mnt/c/Users/xxxxx/javascript/test2.js:62:19
    at PythonShell._endCallback (/mnt/c/Users/xxxxx/javascript/node_modules/python-shell/index.js:254:20)
    at terminateIfNeeded (/mnt/c/Users/xxxxx/javascript/node_modules/python-shell/index.js:209:39)
    at ChildProcess.<anonymous> (/mnt/c/Users/xxxxx/javascript/node_modules/python-shell/index.js:182:13)
    at ChildProcess.emit (events.js:400:28)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:282:12)

can you help me with this? I work with Ubuntu Bash for Windows 10, I don't know if this information is relevant or not...

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I finally resolved my issue by

1) in the python script, I print the dictionary without converting it into a json object. When python is called in javascript it returns a string like below

result: {'brand': 'Ford', 'model': 'Mustang', 'licensePlate': 'O XXX YYY', 'year': 1964, 'insured': {'lastname': 'Snow', 'firstname': 'John', 'adress': 'Holiday street, 4 - Paradise'}}

this string contains single quotes.

2) In javascript, I replace all single quotes with double quotes for parsing this string to a json object.

let parsedData = JSON.parse(JSON.stringify(result.join('\n')));
parsedData = parsedData.replace(/'/g, `"`); // replace the quote by the double quotes for parsing the string to a JSON object.
parsedData = JSON.parse(parsedData);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!