简体   繁体   中英

JSON.parse() is giving unexpected error : "Unexpected end of JSON input"

console.log(d); gives the following output (hexadecimal code):

<Buffer 7b 22 73 75 63 63 65 73 73 22 3a 74 72 75 65 2c 22 64 61 74 61 22 3a 7b 22 73 75 6d 6d 61 72 79 22 3a 7b 22 74 6f 74 61 6c 22 3a 31 39 39 32 35 36 30... 293 more bytes>

<Buffer 6f 62 61 72 20 49 73 6c 61 6e 64 73 22 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 49 6e 64 69 61 6e 22 3a 36 30 38 34 2c 22 63 6f 6e 66 69 72 6d... 1319 more bytes> <Buffer 43 61 73 65 73 49 6e 64 69 61 6e 22 3a 35 39 34 36 30 31 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 65 69 67 6e 22 3a 31 2c 22 64 69 73... 1319 more bytes>

<Buffer 38 2c 22 63 6f 6e 66 69 72 6d 65 64 43 61 73 65 73 46 6f 72 65 69 67 6e 22 3a 33 2c 22 64 69 73 63 68 61 72 67 65 64 22 3a 33 39 38 31 36 35 38 2c 22... 1319 more bytes>

<Buffer 61 6c 43 6f 6e 66 69 72 6d 65 64 22 3a 31 32 30 37 31 31 32 7d 2c 7b 22 6c 6f 63 22 3a 22 54 65 6c 61 6e 67 61 6e 61 22 2c 22 63 6f 6e 66 69 72 6d 65... 740 more bytes>

When I try to convert this into JSON using JSON.parse(d), I get the following error:

undefined:1

{"success":true,"data":{"summary":{"total":19925604,"confirmedCasesIndian":19925556,"confirmedCasesForeign":48,"discharged":16293003,"deaths":218959,"confirmedButLocationUnidentified":0},"unofficial-summary":[{"source":"covid19india.org","total":7945975,"recovered":7198877,"deaths":119538,"active":626192}],"regional":[{"loc":"Andaman and Nicoba

SyntaxError: Unexpected end of JSON input

at JSON.parse (<anonymous>)

at IncomingMessage.<anonymous> (E:\Web Development\Test\app.js:16:24)

at IncomingMessage.emit (events.js:315:20)

at IncomingMessage.Readable.read (internal/streams/readable.js:519:10)

at flow (internal/streams/readable.js:992:34)

at resume_ (internal/streams/readable.js:973:3)

at processTicksAndRejections (internal/process/task_queues.js:80:21)

Here is my app.js file code:

const express = require("express");
const https = require("https");

const app = express();


app.get("/", (req, res) => {

  const url = "https://api.rootnet.in/covid19-in/stats/latest";

  https.get(url, function(response) {
    console.log(response.statusCode);

    response.on("data", (d) => {

      console.log(JSON.parse(d));
      // console.log(d);

    })

  res.send()

  })



})



app.listen(3000, () => {
  console.log("server up and running at port 3000");
})

The method.on("data") receives chunks of buffers, you need to concat that buffers once receiving is finished, convert to string and then parse to JSON.

https.get(url, function(response) {
    const data = [];
    response.on("data", (d) => {
        data.push(d);
    }).on('end', function() {
        //at this point data is an array of Buffers
        //so Buffer.concat() can make us a new Buffer
        //of all of them together
        const buffer = Buffer.concat(data);
        const obj = JSON.parse(buffer.toString());
        console.log(obj);
    });
})

Even though the question is about https

To make things easier, may I suggest to use node-fetch (and/or axios ) instead of https, since it's commonly used (and easier since you don't have to check for start or end of data).

const fetch = require('node-fetch')

......

const results = await fetch(url).then(res => res.json());
console.log(results)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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