简体   繁体   中英

Unexpected token D in JSON at position 1 error

I am calling a python script from node.js as a child process, the python script extracts data from a file I upload to my app, when I upload the file through the app I get the following error ' UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token D in JSON at position 1", but when I test the python script on a data sample declared inside it works just fine, How do I fix this?

Here is the code I used :

import sys
import re
import json
from pathlib import Path
from collections import defaultdict


file_path = (Path(__file__).parent / "../config/kal_ejournal.json").absolute()
print(file_path)
with open(file_path) as jsonFile:
    jsonObject = json.load(jsonFile)


rCARD_NUMBER = jsonObject['Dictionary']['rCARD_NUMBER']['regex']

bCARD_NUMBER = jsonObject['Field_extraction_rules']['bCARD_NUMBER']

regex = rCARD_NUMBER*bCARD_NUMBER 
# re.DOTALL to match any characters including newline
input = open(sys.argv[1], "r")
# print(input.read())
matches = re.findall(regex, input.read(), re.DOTALL)
print(json.dumps(matches))

Here is the code from Node.js

 const python = spawn("python", [
      "./data/DataExtractor_2.py",
      req.file.path,
    ]);

    // collect data from script
    python.stdout.on("data", function (data) {
      console.log("Pipe data from python script ...");
      largeDataSet.push(data);
    });

    // in close event we are sure that stream is from child process is closed
    python.on("close", async (code) => {
      console.log(`child process close all stdio with code ${code}`);
        const pythonRes = largeDataSet.join("");
      var json = JSON.parse("[" + pythonRes + "]");
      var json = [].concat.apply([], json);
      const tocsv = ConvertToCSV(json);
      let tojson = await csv().fromString(tocsv); // convert csv to json
}

You printed out the filename print(file_path) in your python code.

Since you started the python script using node: "data" that outputs from python will be accumulated from within your node data event. Here:

python.stdout.on("data", function (data) {
  console.log("Pipe data from python script ...");
  largeDataSet.push(data);
});

So largeDataSet will almost certainly contain the filename too. Which is bad when you go to parse it.

Assuming this is not actual code and that there is some reason that you prefer not to just do everything in python or node:

You will need to make sure that you are not printing anything from python that is not JSON. That much was clear from the error described. It was just a matter of knowing what JSON was trying to parse. Which you may have already determined on your own from the comments.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM