Bug Tracker

Ticket #12666 (closed bug: notabug)

Opened 2 years ago

Last modified 2 years ago

$.parseJSON fails for valid json string if it has newlines

Reported by: theom3ga@… Owned by:
Priority: undecided Milestone: None
Component: core Version: 1.8.0
Keywords: Cc:
Blocking: Blocked by:

Description

I think I've found a problem in jQuery's parseJSON function.

Suppose I have the following JSON:

    {
        "hello": "hi\nlittle boy?"
    }

If you check it using  JSONlint you can see it's valid JSON. However, if you try the following, you get an error message:

$(function(){
    try{
        $.parseJSON('{ "hello": "hi\nlittle boy?" }');
    } catch (exception) {
        alert(exception.message);
    }    
});​

The  Official JSON Website says that the "\n" character is a valid character inside a JSON string.

Change History

comment:1 Changed 2 years ago by theom3ga@…

I've uploaded a test to  jsFiddle.

comment:2 Changed 2 years ago by gibson042

  • Status changed from new to closed
  • Resolution set to notabug
  • Component changed from unfiled to core

 http://json.org/ specifies that strings cannot contain unescaped control characters like newline. If you're specifying data like that, you must escape the backslash so the JSON parser sees it instead of the newline:  http://jsfiddle.net/m8t89/3/ .

Note: See TracTickets for help on using tickets.