$.parseJSON("1")
returns 1
. I would expect this to throw an error because this does not seem like valid JSON of the form:
{
"firstName": "John"
}
Why does 1
parse correctly? Is there anyway to get this to throw an error instead.
$.parseJSON("1")
returns 1
. I would expect this to throw an error because this does not seem like valid JSON of the form:
{
"firstName": "John"
}
Why does 1
parse correctly? Is there anyway to get this to throw an error instead.
Although 1
isn't a valid JSON object, it is a valid JSON number. It seems that $.parseJSON
parses all JSON values, not just objects.
You can better handle the parsing of numbers using parseInt()
. It will return a number on success and NaN
(Not a Number) otherwise.
var a = parseInt('23');
isNan(a); // false
var b = parseInt('ab');
isNan(b); // true
If you have a look at the source of the jQuery method it will become clear very quickly.
So if in your case step 2.
is executed it will simply return 1
even though it's not real JSON.
UPDATE:
I was curious how the native JSON.parse
would handle it and it does the same thing (returning 1
). So regardless of the implementation you always get the same result.
Library on display: http://code.jquery.com/jquery-1.8.3.js
parseJSON: function( data ) {
if ( !data || typeof data !== "string") {
return null;
}
// Make sure leading/trailing whitespace is removed (IE can't handle it)
data = jQuery.trim( data );
// Attempt to parse using the native JSON parser first
if ( window.JSON && window.JSON.parse ) {
return window.JSON.parse( data );
}
// Make sure the incoming data is actual JSON
// Logic borrowed from http://json.org/json2.js
if ( rvalidchars.test( data.replace( rvalidescape, "@" )
.replace( rvalidtokens, "]" )
.replace( rvalidbraces, "")) ) {
return ( new Function( "return " + data ) )(); // Just returns JSON data.
}
jQuery.error( "Invalid JSON: " + data );
},
parseJSON
actually just returns the JavaScript object from a well formed JSON string.
The JSON format accepts more than just (associative) arrays. It accepts data structures like:
Take a look at http://json.org/ for all the details concerning JSON.
$.parseJSON("1")
actually reads a valid JavaScript number 1, resulting into 1.
1
is not a valid "JSON text", but most JSON parsers accept it anyway. Not all do, as you found with jsonlint.
I posted a more complete explanation with information from the JSON RFC along with Douglas Crockford's opinion in response to another question.
try {
// JSON.parse accepts numbers but we do not want to do that
if (!isNaN(parseInt(inputState))) {
throw "Invalid JSON.";
}
// there we have valid JSON without number
let validJSON = JSON.parse(inputState);
} catch (error) {
console.log(error)
}
I will try to answer it conceptually...
Json represents the value of a certain variable (without the variable name). If the variable is a structure, it will be represented by a Json structure. Same for array, and same for strings and numbers.
Since Json represents a value, you can use it the whole string as a value for a field in a struct, or as a cell in array.
There is a common misconception that Json stores multiple variables and their corresponding value. This is not the case. Json stores a single value, which could be a complex structure, or a simple integer.