Skip to main content

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
}
Via Active questions tagged javascript - Stack Overflow https://ift.tt/2FdjaAW

Comments