JSON values 1 or 0 - int or boolean

JsonIntegerBoolean

Json Problem Overview


Does JSON treat these all the same? Or are they a mix of Integers and booleans?

var data =
{
    "zero" : 0,
    "one" : 1,
    "false" : 0,
    "true" : 1,
    "0" : false,
    "1" : true
}

Json Solutions


Solution 1 - Json

The values true and false are actual boolean values, the rest are integers. See http://json.org/ for more.

Solution 2 - Json

JSON is a format for transferring data.
It has no notion of equality.

JSON parsers treat booleans and numbers as distinct types.

Solution 3 - Json

I prefer using 0/1 instead of true/false, because 0/1 consume only 1 byte while true/false consume 4/5 bytes.

Solution 4 - Json

As mentioned, at JSON level, 0 and false are not the same; data types are number versus boolean. But JSON processing libraries can choose to do conversions; especially on languages/platforms that do not have native boolean type, for example. In that case, another representation may be used (empty string or 0 for false).

Further, it is also possible that processing libraries can coerce types: such that if a boolean value is expected, certain number/string values (or JSON 'null' token) can be accepted instead. This is fairly common, due to differences on data type choices on different languages.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPhill PaffordView Question on Stackoverflow
Solution 1 - JsonRob AgarView Answer on Stackoverflow
Solution 2 - JsonSLaksView Answer on Stackoverflow
Solution 3 - JsonHieu VoView Answer on Stackoverflow
Solution 4 - JsonStaxManView Answer on Stackoverflow