Master Web Development & Programming!
Join Ram N Java for crystal-clear, step-by-step programming guides and modern coding tutorials!
Subscribe to YouTube ChannelIntroduction to JSON Data Types
JSON (JavaScript Object Notation) has become the global standard for sending data across the web. Whether you are consuming a third-party REST API or transferring configuration data between microservices, understanding which data types JSON supports is essential.
Unlike languages like Java or C++ that offer a wide range of primitives and complex objects, JSON keeps things refreshingly simple. It strictly supports six core data types. Let's explore each of them in detail.
The 6 Valid JSON Data Types
-
1. String: A sequence of zero or more Unicode characters wrapped strictly in double quotes (e.g.,
"name": "Alex"). Single quotes are not valid in JSON. -
2. Number: Can be an integer or a floating-point number (e.g.,
"age": 28,"price": 19.99). JSON does not distinguish between integer and float types. -
3. Boolean: Represents logical truth values: either
trueorfalse(written in lowercase, without quotes). -
4. Null: Represents an empty or nonexistent value using the keyword
null. -
5. Object: An unordered collection of key/value pairs enclosed in curly braces
{ }(e.g.,"address": { "city": "New York", "zip": "10001" }). -
6. Array: An ordered list of zero or more values enclosed in square brackets
[ ](e.g.,"skills": ["Java", "JSON", "SQL"]).
What Is NOT Allowed in JSON?
Because JSON is designed strictly as a data-interchange format and not a programming language, certain elements common in JavaScript and other languages cannot be stored directly:
- Functions: Executable code cannot be serialized or stored inside JSON values.
- Dates: There is no native Date type; dates are typically stored as ISO-8601 strings or epoch timestamps.
- Undefined: The value
undefinedis invalid in JSON. - Comments: Standard JSON does not permit single-line or multi-line comments.
Quick Best Practices for Clean JSON
- Always wrap both keys and string values in standard double quotes (
" "). - Avoid trailing commas after the last item in objects or arrays.
- Validate your JSON payloads using online tools or linters before passing them to APIs.
Related Video Tutorials from Ram N Java
Deepen your JSON and web development skills with these tutorials:
No comments:
Post a Comment