JSON and Fields with Carriage Returns

Posted on 2009-04-28
Last Modified: 2013-11-08
I am using JSON with AJAX. There are two problems when sending data from a textarea.

1) If there is no carriage return manually entered in the text area, the last line is not inserted into the database

2) when reading the data back from the db, the JSON looks something like this:
{textareadata : "hello
this is me"

and the browser hangs.

How do I get around this?
Question by:dignified
    LVL 40

    Expert Comment

    Try converting the physical \r and \n into "\r" and "\n"...

    In PHP ...

    $s_Text = <<<END_TEXT
    Line 1
    Line 2
    Line 3
    echo json_encode($s_Text);

    outputs ...

    "Line 1\r\nLine 2\r\nLine 3"

    I'm on windows, so \r\n is normal for me. On *nix systems \r only. Not sure about other OS's.
    LVL 18

    Expert Comment

    I usally replace \r and \n to the unicode line feed or a carriage return:   \u000A or \u000D.  So my code may look something like this:

    string safeForJSON = unicodeReturn.Replace(escapeData, "\\u000A");

    and my json data may look like this:
    This is my json data. \u000A\u000AI am a scary badger...growl!

    Accepted Solution

    Only thing that worked was updating PHP and using their json_encode() function. MSIE was the big culprit having problems.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Introduction HyperText Transfer Protocol ( or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
    I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now