Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

799
Views
JAVASCRIPT JSON.parse() return Unexpected end of JSON input

I have a response from a service, using https

rest.on("data", data => {
  response = JSON.parse(data);
});

I need parse this response but... I know the final result should be(I get this with postman)...

{ "code": 200,
"msg": "",
"data": [
    {
        "id": "t4ba",
        "devData": {
            "id": "bb2as",
            "state": "OK"
        },
        "entries": {
            "2019-05-26T19:03:13.9260000": 15,
            "2019-05-26T19:03:29.1690000": 26,
            "2019-05-26T19:04:16.6880000": 65
        }
    },
    {
        "id": "t4ba-2",
        "devData": {
            "id": "bb2as",
            "state": "Underperformance"
        },
        "entries": {
            "2019-05-26T19:03:13.9260000": 25,
            "2019-05-26T19:03:29.1690000": 45,
            "2019-05-26T19:04:16.6880000": 32
        }
    }
],
"dateResponse": "2021-03-26T19:04:16.6880000" }

When I run JSON.parse(data)

I get this errror

"errorType": "SyntaxError", "errorMessage": "Unexpected end of JSON input", "trace": [ "SyntaxError: Unexpected end of JSON input", " at JSON.parse ()", " at IncomingMessage. (/var/task/index.js:47:23)", " at IncomingMessage.emit (events.js:315:20)", " at IncomingMessage.EventEmitter.emit (domain.js:467:12)", " 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)" ]

I think the "data array" field is the problem and I don't know how to solve it.

When I receive a response with empty data:[], everything works fine.

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Hmmmm... The answer is easy...

Lets Start:::::

First, looks like you get a lot of data from your service...

On response.on("data"

Data is not complete at first, so, .on("data" "repeat himself" from init to end, then ur data is complete...

code

res.on("data", data => {
  someVariable += data;
});

then... in your .on("end"

rest.on("end", () => {
  MyJSON = JSON.parse(someVariable);
});

That's all folks.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!