4

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.

8
  • 2
    Why to parse, when it is already an object. Commented Mar 27, 2021 at 1:29
  • 1
    the JSON you've posted is valid - can you confirm that response is the string you've posted Commented Mar 27, 2021 at 1:30
  • @DeC - what makes you believe it is already an object Commented Mar 27, 2021 at 1:32
  • 1
    @JaromandaX codepen.io/kumarmasterpraveen/pen/jOyqxPQ?editors=0011 Commented Mar 27, 2021 at 1:34
  • The data itself looks fine. What are you using to make this request? Based on the error stack, seems like possibly a vanilla node request or the request is being streamed in. If so, you would need to wait for the end event to parse the data. Something like: vanillajstoolkit.com/polyfills/stringreplaceall/ Commented Mar 27, 2021 at 1:54

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

THANKS! I spend a lot of time with that, Im really glad, THANKS!
Try look alternative for pure node request, outside are a lot of methods with better results. Remember close ur answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.