Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have to parse a json formatted as done below! I have to parse two data, one of which is an array! But when I do it, the error below is generated. How can I solve?


Node.js Code:
JavaScript
app.post("/rapportini/generarapportino",async function(request,response)
{
    console.log("\n Qualcuno sta generando un rapportino");
    try
    {
        console.log(request.body);
        var note = JSON.parse(Object.keys(request.body)[0]);
        var articoli=JSON.parse(Object.keys(request.body)[1]); //This is array
        const ret=await RapportiniController.GeneraRapportino(note,articoli);
        response.setHeader('Content-Type', 'application/json');
        response.send(JSON.stringify({ return: ret }));
    }

    catch(err){
        console.log("Errore generazione rapportino ",err)
    }

});




JSON:
{ '{"Note":"Sa ","Articoli":':
   { '{"Fornitore":"COMET","Prezzo":48.850000000000001,"CodArt":"GW.2000","Importato":"COMET","PrezzoListino":58.149999999999999,
"IdArticolo":8013,"CodMarca":"ARE","Descrizione":"LANTERNA RICARICABILE NEW LED"},{"Fornitore":"COMET","Prezzo":48.85000000000000
1,"CodArt":"GW.2000","Importato":"COMET","PrezzoListino":58.149999999999999,"IdArticolo":8013,"CodMarca":"ARE","Descrizione":"LAN
TERNA RICARICABILE NEW LED"},{"Importato":"100Impianti","Fornitore":"Cento Impianti S.r.l.","IdArticolo":87,"Descrizione":"CAVO A
LLARME 6X022   2X075","Prezzo":0.45900000000000002,"CodArt":"I355IT"}': '' } }


Error:
SyntaxError: Unexpected end of JSON input
    at JSON.parse (<anonymous>)


What I have tried:

i try update npm package
Posted
Updated 27-May-18 23:29pm
v2

1 solution

Your JSON is not in a valid format, which is more obvious when you look at the syntax colouring.

It should probably be like this:

{
  "Note": "Sa ",
  "Articoli": [
    {
      "Fornitore": "COMET",
      "Prezzo": 48.85,
      "CodArt": "GW.2000",
      "Importato": "COMET",
      "PrezzoListino": 58.15,
      "IdArticolo": 8013,
      "CodMarca": "ARE",
      "Descrizione": "LANTERNA RICARICABILE NEW LED"
    },
    {
      "Fornitore": "COMET",
      "Prezzo": 48.85,
      "CodArt": "GW.2000",
      "Importato": "COMET",
      "PrezzoListino": 58.15,
      "IdArticolo": 8013,
      "CodMarca": "ARE",
      "Descrizione": "LANTERNA RICARICABILE NEW LED"
    },
    {
      "Fornitore": "Cento Impianti S.r.l.",
      "Prezzo": 0.459,
      "CodArt": "I355IT",
      "Importato": "100Impianti",
      "IdArticolo": 87,
      "Descrizione": "CAVO ALLARME 6X022   2X075"
    }
  ]
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900