JSON null and Boolean
JSON supports exactly two special literal values: null and booleans (true and false). These three values look simple, but they carry important meaning in real applications. Misusing them causes bugs that are hard to find.
What is null in JSON
The value null in JSON means "no value" or "empty on purpose." It is different from zero, an empty string, or a missing key. It is an explicit signal that a field exists but has no data.
Think of a form with a field called "Middle Name." Some people have a middle name and some do not. If the field is left empty on purpose, the value is null. If the field is missing entirely from the form, that is a different situation.
null in JSON: Syntax
{
"firstName": "Arjun",
"middleName": null,
"lastName": "Sharma"
}
The key middleName is present. Its value is null. This tells the application: "I know about this field. The person has no middle name."
null vs Missing Key
// Case 1: Key exists with null value
{
"middleName": null
}
// Case 2: Key is completely absent
{
"firstName": "Arjun"
}
In Case 1, the application knows the field exists but is empty. In Case 2, the application has no information about middle name at all. This distinction matters in database updates and API design.
Diagram: null vs Empty vs Missing
Think of a desk drawer labeled "Passport"
Drawer labeled "Passport" with nothing inside
= null (the field exists, value is absent)
Drawer labeled "Passport" with an empty envelope
= "" (the field exists, value is an empty string)
No drawer labeled "Passport" at all
= missing key (the field is not mentioned)
Important Rules for null
nullmust be written in lowercase.NullorNULLare invalid in JSON.nullhas no quotes around it."null"is a string containing the word null, which is completely different.nullis a valid value for any JSON field regardless of its normal data type.
Invalid null Examples
{
"score": Null, // Invalid - uppercase N
"score": NULL, // Invalid - all uppercase
"score": "null" // Valid JSON, but this is a STRING, not null
}
Boolean Values in JSON
JSON has two boolean values: true and false. These represent yes/no or on/off states. Boolean values are used for flags, settings, and conditions.
Boolean Syntax
{
"isActive": true,
"isDeleted": false,
"hasSubscription": true
}
Boolean Rules
trueandfalsemust be lowercase.True,False,TRUE,FALSEare all invalid.- No quotes around booleans.
"true"is a string, not a boolean. - No numbers like 1 or 0 — JSON does not treat 1 as true.
Invalid Boolean Examples
{
"isActive": True, // Invalid - capital T
"isActive": "true", // This is a string, not a boolean
"isActive": 1 // This is a number, not a boolean
}
Diagram: The Three Literal Values in JSON
JSON Literals
|
+---> null (no value, written as: null)
|
+---> Boolean (true/false, written as: true or false)
|
(These are the only three non-string, non-number primitive literals)
Real-World Uses of null and Boolean
User Profile API Response
{
"userId": 1042,
"name": "Priya Mehta",
"email": "priya@example.com",
"phoneNumber": null,
"isEmailVerified": true,
"isPhoneVerified": false,
"isPremiumMember": false,
"deletedAt": null
}
This shows a user who verified their email but not their phone. The phone number is null because they did not provide one. The deletedAt field is null because the account is still active.
Product Inventory
{
"productId": "SKU-991",
"name": "Laptop Stand",
"inStock": true,
"discountPrice": null,
"isFeatured": false
}
The product is in stock but has no discount currently. The discountPrice is null, not zero. A price of zero would mean it is free. Null means no discount applies.
Common Mistakes and How to Fix Them
Mistake 1: Using 0 Instead of null
// Wrong - ambiguous
{ "score": 0 }
// Better - explicit about no score
{ "score": null }
A score of 0 could mean the player scored zero points, or it could mean no score was recorded. Use null to show no score was recorded.
Mistake 2: Using "false" as a String
// Wrong - this is the string "false", not a boolean
{ "isActive": "false" }
// Correct - this is the boolean false
{ "isActive": false }
Many applications that receive "false" as a string will treat it as truthy in JavaScript because any non-empty string is truthy. This causes serious bugs.
Mistake 3: Confusing null with Empty String
// The field exists but has no meaningful value
{ "bio": null }
// The field exists and the user actively wrote nothing
{ "bio": "" }
In a profile bio field, null means the user never touched the field. An empty string means the user opened the bio editor and saved with no text.
Checking for null in JavaScript
const user = JSON.parse('{"name": "Raj", "phone": null}');
if (user.phone === null) {
console.log("Phone number not provided");
}
// Using typeof with null - a classic JavaScript quirk
console.log(typeof null); // outputs "object" — this is a JavaScript bug, not JSON behavior
Always use === null to check for null in JavaScript. Do not use typeof for null checks because JavaScript incorrectly reports typeof null as "object".
Checking for Boolean in JavaScript
const settings = JSON.parse('{"darkMode": true, "notifications": false}');
if (settings.darkMode === true) {
console.log("Dark mode is ON");
}
if (!settings.notifications) {
console.log("Notifications are turned off");
}
null and Boolean in Python
Python uses different names for these values internally. When you parse JSON in Python, the conversion happens automatically.
import json
data = json.loads('{"active": true, "score": null, "enabled": false}')
print(data["active"]) # True (Python uses capital T)
print(data["score"]) # None (Python uses None for null)
print(data["enabled"]) # False (Python uses capital F)
JSON true becomes Python True. JSON false becomes Python False. JSON null becomes Python None. When you convert back to JSON, Python automatically reverses this mapping.
Quick Reference Table
JSON Value JavaScript Python Meaning
---------- ---------- ------ -------
null null None No value / empty on purpose
true true True Yes / on / active
false false False No / off / inactive
Summary
JSON's null, true, and false are simple but important. Always write them in lowercase. Never quote them unless you want a string. Use null deliberately to signal "this field has no value" rather than defaulting to zero or empty string. Understanding these distinctions makes your JSON data more precise and your applications more reliable.
