Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:35:53+00:00 2026-05-31T13:35:53+00:00

I’m retrieving some stringifyed JSON via TCP in node.js an want to parse it.

  • 0

I’m retrieving some stringifyed JSON via TCP in node.js an want to parse it. So my approach is similiar to this. I shortend and simplified it, so you don’t have to know the surrounding logic.

socket.on("data", function(data) {
    console.log(data.toString());               // Shows the original stringifyed version
    console.log(JSON.parse(data.toString()));   // Doesn't work
});

The complete (beautified) JSON is this. As you can see, there are no errors.

{
    "result": "success",
    "source": "chat",
    "success": {
        "message": "test",
        "time": 1331770513,
        "player": "silvinci"
    }
}

But JSON.parse(data.toString()) always throws me this dumb error:

{"result":"success","source":"console","success":{"time":1331762264,"line":"20
^
SyntaxError: Unexpected token {
    at Object.parse (native)
    at Socket.<anonymous> (/home/node/api.js:152:35)    // irrelevant from here on
    at Socket.emit (events.js:67:17)
    at TCP.onread (net.js:347:14)

So I thougt: “What could be wrong with the JSON-String. Let’s try it directly. Should not work.” Surprise, Surprise! It worked. Why does it work when I directly enter the String?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:35:54+00:00Added an answer on May 31, 2026 at 1:35 pm

    Thanks to @Felix Kling I found my bug. It’s very important to filter unescaped characters, especially outside the stringified JSON. I didn’t and overlooked an invisible linebreak right after the stringified JSON.

    This is the fix:

    socket.on("data", function(data) {
        console.log(data.toString());                          // Shows the original stringified version
        console.log(JSON.parse(data.toString().slice(0, -4))); // Trim the sequence "\r\n" off the end of the string
    });
    

    Please note, that this only works for me, as I have a very specialized case. The server is always responding in lines of JSON, that are terminated with an \r\n – not the whitespace characters, but literally backslash r and backslash n.
    Your code may (or probably) fail due to other errors. But checking the server’s response is a good starting point when you get parsing errors.

    As @Zack correctly pointed out, this is a more general fix for removing unintended whitespace:

    JSON.parse(data.toString().trim());
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the
I have some data like this: 1 2 3 4 5 9 2 6
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.