Im trying to send a json from Flask to NodeJS and respond a pdf. The json contains data necessary for making receipts used by the walking collector and its basic structure looks like this:
{"list":[{"user":"user1","month":"11/2020"},{"user":"user1","month":"12/2020"}],qid:"query_id"}
This works well but only if the json is small enough (about 500 elements in the inner list ) if i were to pass a bit more elements (like 600/700) it will break with the following:
SyntaxError: Unexpected token , in JSON at position 98224
After a trip to JSONLint found out the culprit in the list:
{ "NOMBRE": "JUAN ", "NROSOCIO": 2706, "DIRECCION": "fake_street 587 ", "RECARGO": 0, "IMPORTE": 50, "PERIODO": "12/2020", "BARCODE": "027061220201000050.0", "COBRADOR", : "1" }
The problem its the random ,
before :
in COBRADOR
but i dont know why its (consistently) there
Things i tried
- Replacing all the data with static values
- Not putting the "COBRADOR" element in the json (the comma still apears)
- Transforming the buffer to string before parsing in Node
Python code for generating the json
for socio in db.session.query(socios).filter(socios.COBRADOR == cobrador).all():
mes = int(mesi)
año = int(añoi)
for i in range(meses):
barcode = cuponificador(str(socio.NROSOCIO),5)+cuponificador(str(mes),2)+str(año)+str(socio.COBRADOR)+cuponificador(str(importe),8)
periodo = str(mes)+"/"+str(año)
ret.append(({"NOMBRE":socio.NOMBRE,"NROSOCIO":socio.NROSOCIO,"DIRECCION":socio.DIRECCION,"RECARGO":ceil(float(recargo)),"IMPORTE":ceil(float(importe)),"PERIODO":periodo,"BARCODE":barcode,"COBRADOR":cobrador}))
mes = mes + 1
if (mes == 13):
mes = 1
año = año + 1
req = requests.post("http://localhost:3000/", json= ({"list":ret,"tipo":"socios"}))
NodeJs code
const server = http.createServer((req, res) => {
let data = []
req.on('data', chunk => {
data.push(chunk)
})
req.on('end', () => {
console.log("LLego pedido")
console.log(data.toString('utf8'))
jason = JSON.parse(data.toString('utf8')) //here breaks
//jason = JSON.parse(data) //also tried this
cupones = jason.list
tipo = jason.tipo
make(cupones, tipo).then(function() {
var file = fs.createReadStream('./document.pdf');
file.on('open', function () {
var stat = fs.statSync('./document.pdf');
res.statusCode = 200;
res.setHeader('Content-Length', stat.size);
res.setHeader('Content-Type', 'application/pdf');
file.pipe(res);
});
})
})});
Why are those commas coming up and how do it stop it?