简体   繁体   English

JSON 中的意外令牌 D 位于位置 1 错误

[英]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?我从 node.js 调用 python 脚本作为子进程,python 脚本从我上传到我的应用程序的文件中提取数据,当我通过应用程序上传文件时,我收到以下错误' UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token D在 JSON 中的位置 1",但是当我在内部声明的数据样本上测试 python 脚本时,它工作得很好,我该如何解决这个问题?

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这是来自 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.您在 python 代码中打印出文件名print(file_path)

Since you started the python script using node: "data" that outputs from python will be accumulated from within your node data event.由于您使用节点启动了 python 脚本:从 python 输出的“数据”将从您的节点数据事件中累积。 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.所以largeDataSet几乎肯定也会包含文件名。 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:假设这不是实际代码,并且出于某种原因您不希望只在 python 或节点中执行所有操作:

You will need to make sure that you are not printing anything from python that is not JSON.你需要确保你没有从 python 打印任何不是 JSON 的东西。 That much was clear from the error described.从所描述的错误中可以清楚地看出这一点。 It was just a matter of knowing what JSON was trying to parse.这只是知道 JSON 试图解析什么的问题。 Which you may have already determined on your own from the comments.您可能已经从评论中自行确定。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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