JSON Data Types Explained for Developers
Complete guide to JSON data types: strings, numbers, booleans, null, objects, and arrays. Understand how each type maps to TypeScript and C# when generating type definitions.
The Six JSON Data Types
JSON (JavaScript Object Notation) has exactly six data types. Understanding them is essential for generating accurate type definitions.
1. String
Strings are enclosed in double quotes. They support Unicode and escape sequences.
{ "name": "Alice", "emoji": "👋", "path": "C:\\Users" }
| Target | Generated Type |
|---|---|
| TypeScript | string |
| C# | string |
2. Number
JSON doesn't distinguish between integers and floats — both are "number." Our converter infers the distinction from the value:
{ "count": 42, "price": 19.99, "scientific": 1.5e10 }
| Value | TypeScript | C# |
|---|---|---|
| Integer (42) | number | int |
| Decimal (19.99) | number | double |
3. Boolean
Simple true/false values:
{ "isActive": true, "isDeleted": false }
| Target | Generated Type |
|---|---|
| TypeScript | boolean |
| C# | bool |
4. Null
Null represents the absence of a value. It's critical for type generation because it signals optionality:
{ "middleName": null, "deletedAt": null }
| Target | Generated Type |
|---|---|
| TypeScript | Optional (?) with ` |
| C# | Nullable (? for value types) |
5. Object
Objects are key-value pairs enclosed in curly braces. Keys must be strings:
{
"address": {
"street": "123 Main St",
"city": "Portland"
}
}
Each object becomes a separate interface (TypeScript) or class (C#) with its own named type.
6. Array
Arrays are ordered lists enclosed in square brackets. They can contain any JSON type:
{
"tags": ["dev", "prod"],
"scores": [95, 87, 92],
"matrix": [[1, 2], [3, 4]],
"mixed": [1, "two", null]
}
| Array Type | TypeScript | C# |
|---|---|---|
| Homogeneous | string[] | List<string> |
| Objects | Item[] | List<Item> |
| Mixed | (type1 | type2)[] | List<object> |
| Empty | unknown[] | List<object> |
Type Inference Strategy
Our converter uses these rules to infer types:
- Examine all elements in arrays to determine the element type
- Merge object shapes when multiple objects appear in the same array
- Mark as optional when a field is null or missing from some array elements
- Create separate types for each unique nested object structure
- Use union types in TypeScript when array elements have different types
Understanding these rules helps you predict and validate the generated output.