0

I am using python shell npm package to invoke the python scripts from node.js. My python Script returns json object which looks like this:

{"timestamp":"2005-10-30 10:45:33","error_code":"0","error_code_string":"Passed","mac_address":"00:0D:6F:41:AA:4A"}

When i run the below code i get the results in the form of array:

const {PythonShell} = require("python-shell")
// import {PythonShell} from 'python-shell';

function Runpy(){

 const options = {
   mode: 'text',
   pythonPath: 'C:/Users/xyz/AppData/Local/Programs/Python/Python38/python3',
   pythonOptions: ['-u'],
   scriptPath: 'D:/ABC',
   args : ['-test=3','-scan=200']
 };

 PythonShell.run('test_runner.py', options, function (err, results) {
   if (err) throw err;
   // results is an array consisting of messages collected during execution
   console.log('results: %j', results);
   //parsed = results.toString();
   //let parsedResult = JSON.parse(results);
   console.log(results);
 });
}

return Runpy();

Output:

results: ["{'timestamp': '2005-10-30 10:45:33', 'error_code': '0', 'error_code_string': 'Passed', 'mac_address': '00:0D:6F:41:AA:4A'}"]

[ '{\'timestamp\': \'2005-10-30 10:45:33\', \'error_code\': \'0\', \'error_code_string\': \'Passed\', \'mac_address\': \'00:0D:6F:41:AA:4A\'}' ]

When I try to parse the variable 'results' i get the error:

SyntaxError: Unexpected token ' in JSON at position 1

I thought since results are already an object I am getting the error. So I tried to stringify the results and parse it. I am not getting any error then, but when I access individual items like timestamps by calling results. timestamp, I am getting as undefined.

Can anyone please suggest a way to convert this into JSON?

2
  • 2
    JSON requires double quotes. How did they get from your result above to the one below, turning " to '? Commented Oct 31, 2019 at 7:25
  • @ASDFGerte In the Python Script i am passing the JSON with double quotes, but i think python-shell npm package is replacing the double quotes with single quotes when trying to convert the result into an array. Commented Oct 31, 2019 at 7:35

1 Answer 1

1

The problemcomes from the single ticks '. They need to be double ticks " to be valid json. You can use replace to replace ' with ".

let results = ["{'timestamp': '2005-10-30 10:45:33', 'error_code': '0', 'error_code_string': 'Passed', 'mac_address': '00:0D:6F:41:AA:4A'}"];

results[0] = results[0].replace(/'/g, '"');

let parsed = JSON.parse(results[0])

console.log(parsed)
console.log(parsed.error_code)

Sign up to request clarification or add additional context in comments.

3 Comments

The only real answer/solution is to fix the output on the server.
These types of search-and-destroy replaces should not be used in production code. Imagine `{"a": "mc'cool"}`.
This solution fails if you have single quotes in error_code_string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.