📋">
Validate & Format JSON Data • 2026 Edition
\( \text{Valid JSON: } \{ "key": "value", "array": [1, 2, 3], "nested": \{ "prop": true \} \} \)
Where:
JSON (JavaScript Object Notation) is a lightweight data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is based on a subset of JavaScript programming language and uses key-value pairs to represent data. JSON is widely used for APIs and configuration files.
Example: Valid JSON structure:
\(\{ \)
\("name": "John Doe",\)
\("age": 35,\)
\("active": true,\)
\("hobbies": ["reading", "gaming", "coding"]\)
\(\} \)
Thus, this structure is valid JSON that can be parsed by any JSON-compatible system.
| Metric | Count | Description |
|---|---|---|
| Objects | 2 | Curly brace {} structures |
| Arrays | 1 | Square bracket [] structures |
| Strings | 7 | Text values in quotes |
| Numbers | 1 | Numeric values |
| Booleans | 1 | true/false values |
| Null Values | 0 | null values |
| Keys | 6 | Object property names |
| Total Characters | 208 | Length of formatted JSON |
JSON (JavaScript Object Notation) is a lightweight, text-based data interchange format that is easy for humans to read and write and easy for machines to parse and generate. It is language-independent but uses conventions familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others.
JSON has strict syntax rules that must be followed:
Where:
JSON structures commonly follow these patterns:
Which of the following is a valid JSON string?
The answer is B) "Hello World". In JSON, strings must be enclosed in double quotes. Single quotes (''), backticks (`), and unquoted text are not valid JSON string formats. This is a fundamental rule of JSON syntax that must be followed for valid JSON.
This question highlights one of the most basic but critical JSON syntax rules. Many developers coming from other languages assume single quotes work in JSON, but they don't. The double quote requirement is strict and any deviation results in invalid JSON. This rule exists to ensure unambiguous parsing across all platforms and languages.
JSON String: Text enclosed in double quotes
Double Quote: " character required for strings
Syntax Validation: Checking for proper JSON formatting
• Strings must use double quotes
• Single quotes are invalid in JSON
• Backticks are invalid in JSON
• Always use double quotes for strings
• Remember: 'value' is invalid, "value" is valid
• Use a JSON validator to check syntax
• Using single quotes instead of double quotes
• Forgetting quotes around string values
• Using backticks for strings
Why is the following JSON invalid? { name: "John", age: 30 }
This JSON is invalid because the key "name" is not enclosed in double quotes. In JSON, all keys must be strings and therefore must be enclosed in double quotes. The correct format would be: { "name": "John", "age": 30 }. The same applies to the key "age" - it must also be quoted as "age".
This is a very common mistake, especially for developers familiar with JavaScript object literals. In JavaScript, you can omit quotes around keys if they're valid identifiers, but in JSON, all keys must be quoted strings. This ensures consistent parsing across all languages and platforms that support JSON.
JSON Key: Property name in key-value pair
Quoted Key: Key surrounded by double quotesIdentifier: Valid name in programming languages
• All keys must be quoted strings
• JavaScript objects != JSON
• Strict syntax requirements
• Remember: JSON is stricter than JavaScript
• Always quote your keys
• Use a formatter to verify validity
• Confusing JavaScript objects with JSON
• Forgetting to quote keys
• Assuming similar syntax rules
Create a valid JSON structure for a person with name, age, address (with street, city, zip), and hobbies (array of strings). Show proper nesting.
{
"name": "Alice Johnson",
"age": 28,
"address": {
"street": "456 Oak Ave",
"city": "Springfield",
"zip": "62701"
},
"hobbies": ["photography", "cooking", "hiking"]
}
This structure properly nests the address object within the person object and uses an array for the hobbies, following all JSON syntax rules.
This example demonstrates how JSON supports nested structures, which is one of its key strengths. Objects can contain other objects and arrays, allowing for complex data hierarchies. Each level of nesting must follow the same JSON rules: keys quoted, proper value types, and correct punctuation.
Nested Object: Object contained within another object
Complex Data: Hierarchical information structure
Proper Indentation: Clear formatting for readability
• Maintain consistent indentation
• Properly close all brackets
• Follow syntax at every level
• Use consistent indentation for readability
• Validate at each level of nesting
• Use formatters for complex structures
• Mismatched brackets or braces
• Forgetting commas between items
• Improper nesting structure
A developer receives this JavaScript object and needs to convert it to valid JSON. What changes are required? const obj = { name: 'Bob', details: { age: 35, active: true }, tags: ['admin', 'user'] };
The JavaScript object needs these changes to become valid JSON:
1. Quote all keys: name → "name", age → "age", etc.
2. Change single quotes to double quotes: 'Bob' → "Bob", 'admin' → "admin"
Resulting valid JSON:
{
"name": "Bob",
"details": {
"age": 35,
"active": true
},
"tags": ["admin", "user"]
}
Note that the boolean value 'true' and number '35' don't need changes as they're already valid JSON values.
This question illustrates the common confusion between JavaScript objects and JSON. While JavaScript objects can have unquoted keys and single-quoted strings, JSON requires quoted keys and double-quoted strings. The conversion process involves syntactic changes but preserves the data structure and values.
JavaScript Object: Language-specific data structure
JSON: Language-independent data format
Data Transformation: Converting between formats
• JSON is a subset of JavaScript object notation
• More restrictive than JavaScript objects
• Requires double quotes for strings and keys
• Use JSON.stringify() in JavaScript to convert
• Remember: Valid JSON is always valid JS object
• But valid JS object isn't always valid JSON
• Assuming JavaScript objects are valid JSON
• Forgetting to quote keys during conversion
• Not changing single quotes to double quotes
Which of the following is NOT a valid JSON data type?
The answer is C) Function. JSON only supports these data types: strings, numbers, booleans, objects, arrays, and null. Functions are not a valid JSON data type. This is because JSON is designed as a data interchange format, not a programming language, so executable code (like functions) is not permitted.
This question tests understanding of JSON's limited data type support. While JavaScript (the language that inspired JSON) supports functions, dates, undefined, and other types, JSON as a data format only supports a specific set of types. This limitation ensures that JSON can be reliably parsed and generated across all programming languages and platforms.
JSON Data Types: Limited set of supported values
Function: Executable code (not supported)
Data Interchange: Format for sharing data
• Only 6 data types are valid in JSON
• No executable code allowed
• Strict type limitations for portability
• Remember the 6 valid types: string, number, boolean, object, array, null
• Functions, dates, and undefined are not valid
• Convert complex types before JSON serialization
• Including functions in JSON
• Assuming all JavaScript types are valid
• Forgetting that dates must be strings
Object: \{ "key": "value", "array": [1, 2, 3] \}
Array: [ "item1", "item2", \{ "nested": true \} ]
Correct: \{ "name": "John", "age": 30 \}
Incorrect: \{ name: "John", age: 30 \} (unquoted keys)
Always validate JSON before processing.
Parse: JSON.parse(string) → JavaScript object
Stringify: JSON.stringify(object) → JSON string
Q: What's the difference between JSON and JavaScript objects?
A: While JSON is based on JavaScript object syntax, there are important differences:
JSON Requirements:
JavaScript Objects:
Essentially, JSON is a subset of JavaScript object notation designed for data interchange.
Q: How do I validate JSON programmatically?
A: Here are the most common approaches:
JavaScript Validation:
try {
const parsed = JSON.parse(jsonString);
console.log('Valid JSON:', parsed);
} catch (error) {
console.error('Invalid JSON:', error.message);
}
Python Validation:
import json
try:
parsed = json.loads(json_string)
print("Valid JSON:", parsed)
except json.JSONDecodeError as e:
print("Invalid JSON:", e)
Server-side Validation:
Always validate JSON before processing to prevent errors and security vulnerabilities.