π JSON Boolean β True/False Values in JSON Explained (2025 Guide)
π§² Introduction β What is a Boolean in JSON?
In JSON (JavaScript Object Notation), a boolean is a data type that holds one of two values: true
or false
. Booleans are used to represent binary statesβsuch as on/off, yes/no, enabled/disabledβmaking them essential for decision-making in data structures, APIs, and configurations.
π― In this guide, youβll learn:
- What boolean values are in JSON
- How to write valid booleans
- How JSON booleans are used in real examples
- How they are handled in JavaScript and other programming languages
β JSON Boolean β Format and Syntax
JSON only allows two boolean values:
true
false
π Syntax Rules:
- Must be written in lowercase (
true
,false
) - No quotes around the value (unlike strings)
- Cannot be spelled with capital letters (
True
,False
are invalid) - Cannot use numbers like
1
or0
as a substitute
π§ͺ Example β JSON Object with Booleans
{
"isActive": true,
"isAdmin": false,
"subscribed": true
}
π Explanation:
"isActive": true
β The user is active"isAdmin": false
β The user is not an admin"subscribed": true
β The user has an active subscription
βοΈ JSON Boolean in JavaScript
When you parse a JSON string containing booleans in JavaScript, they are automatically converted to native JavaScript boolean types.
β Example:
const json = '{ "loggedIn": true, "premiumUser": false }';
const user = JSON.parse(json);
console.log(user.loggedIn); // Output: true
console.log(typeof user.loggedIn); // Output: boolean
π Explanation:
JSON.parse()
converts the JSON string into a JavaScript object- Boolean values remain
true
orfalse
, not converted to strings
π« Invalid JSON Boolean Examples
Here are some common mistakes that cause invalid boolean values in JSON:
{
"flag": "true" // β Invalid β value is a string, not a boolean
}
{
"enabled": True // β Invalid β JSON booleans must be lowercase
}
β Corrected Version:
{
"flag": true,
"enabled": false
}
π Summary β Recap & Next Steps
Booleans in JSON are simple and powerful. They let you represent two-state values clearly and reliably across different programming environments.
π Key Takeaways:
- JSON booleans can only be
true
orfalse
(no quotes) - Always write them in lowercase
- Do not use
"true"
as a string or1/0
as a substitute - They automatically map to native booleans in JavaScript and most languages
βοΈ Real-world use:
JSON booleans are widely used in user settings, feature toggles, authentication states, and API flags like "success": true
.
β FAQ β JSON Boolean
β What values are valid booleans in JSON?
β
Only two: true
and false
. Both must be lowercase and unquoted.
β Can I use "true"
as a boolean in JSON?
β No. "true"
is a string, not a boolean. It will be parsed as text.
β Are 1
and 0
valid booleans in JSON?
β No. JSON does not allow using integers as boolean values.
β How do JSON booleans behave in JavaScript?
β
They become native true
or false
boolean values when parsed with JSON.parse()
.
Share Now :