Search code examples
pythonpython-3.xchild-processsys

how to convert buffer stringify from node.js to array in python


I have buffer json and write to python in nodejs this:

const python = spawn("python", ["index.py"]);
//label = ["a","b","c","d"]
let label = Buffer.from(JSON.stringify(label)); //<-----------------HERE
python.stdin.write(label); //<-----------------HERE
python.stdin.end();

and i'm read line in python this :

label = sys.stdin.readlines()
print(label)
#OUTPUT : ['["a","b","c","d"]']

How do I do convert is ['["a","b","c","d"]'] to array in python for use it ? Thanks!


Solution

  • Use json.load:

    import json
    
    label = json.load(sys.stdin)