Easy Tutorial
❮ Event Triggerhandler Sel Input Enabled ❯

jQuery.parseJSON() Method

jQuery Miscellaneous Methods

Example

Parsing a JSON string

$(function () { 
    var obj = jQuery.parseJSON('{"name":"John"}');
    alert( obj.name === "John" );
})

Definition and Usage

The $.parseJSON() function is used to convert a properly formatted JSON string into its corresponding JavaScript object.

Note: Passing an incorrectly formatted JSON string can result in an exception being thrown. For example, the following are invalid JSON strings:

"{test: 1}"    
//test is a property name and must be enclosed in double quotes

"{'test': 1}"    
//test is a property name and must use double quotes (not single quotes)

"'test'" 
//test is a property name and must use double quotes (not single quotes)

".1" 
//a number must start with a digit; "0.1" would be valid

"undefined"    
//undefined cannot represent a JSON string; null can

"NaN" 
//NaN cannot represent a JSON string; using Infinity to directly represent infinity is also not allowed

The JSON standard does not allow "control characters" such as tab characters or newline characters, for example:

//In most cases, it will throw an error because the JS parser interprets escape characters like \t or \n in the string as literal values, resulting in tab or newline effects.
$.parseJSON('{"testing":"1\t2\n3"}')
❮ Event Triggerhandler Sel Input Enabled ❯