JSON data is just text that follows a specific set of rules for structure, making it simple to work with across different platforms and tools. It’s perfect for promoting communication between different programming languages in a multi-system project.
Because JSON is a text-based format and uses a simple structure (name/value pairs), it’s language-agnostic.
This means that the format (in this case, JSON) is not tied to any particular programming language. It can be used by programs written in many different programming languages (like Python, JavaScript, Java, etc.), without needing to be modified or customized for each language.
In other words, JSON is universally compatible with most languages, allowing them to communicate with each other.
Every modern language now, Python, JavaScript, Ruby, Java, C#, all have libraries or built-in support for working with JSON.
Different systems (written in different languages) can send JSON data to each other, and each system can easily serialize (convert data to JSON) and deserialize (convert JSON back to usable data) the data in its own format.
JSON is commonly used in web APIs (like REST APIs), allowing systems in different languages to communicate over the web using HTTP. For example, a Python server can send JSON data to a JavaScript frontend.
JSON follows a standard format that all systems agree on, which is one of the reasons why it works across languages.
A Python backend might send a JSON object to a JavaScript frontend in a web application. The frontend can then read the JSON, display the data, or pass it along to another system written in Java or C#.
1. Data is in name/value pairs
- A pair is written as
"name": value
. The name is a string, and the value can be a string, number, object, array, boolean, ornull
.
2. Data is separated by commas
- Each name/value pair is separated by a comma. This applies within objects and arrays.
3. Curly braces hold objects
- JSON objects are enclosed in curly braces
{}
. An object is a collection of name/value pairs.
4. Square brackets hold arrays
- JSON arrays are enclosed in square brackets
[]
. An array can contain values of any type, including objects.
5. Strings must be in double quotes
- JSON strings are always surrounded by double quotes
" "
, not single quotes.
6. No trailing commas
- There can’t be a comma after the last name/value pair in an object or array.
7. Only certain data types
- JSON supports only these data types:
- Numbers
- Objects
- Strings
- Arrays
- Booleans (
true
/false
) null
8. No comments allowed
- JSON does not allow comments. Unlike other formats (like YAML or JavaScript), you can’t add notes inside the data.
JSON has some limitations when it comes to the types of data it can represent. You cannot directly pass the following types of data using JSON:
- Functions.
JSON can’t store functions or methods (likedef
in Python or JavaScript functions). You can’t pass behavior, just data. - Dates.
JSON doesn’t have a built-in type for dates or timestamps. If you need to pass a date, it’s usually converted to a string (like"2025-04-21"
) and handled by the receiving system. - Undefined or NULL.
JSON hasnull
, but undefined (in JavaScript) or NaN (Not a Number) values can’t be directly serialized. They may be converted tonull
or another value. - RegExp (Regular Expressions).
You can’t pass regular expressions directly in JSON. They would need to be converted into a string. - Complex Objects.
Some complex objects, like those from custom classes or native system objects (like file handles, database connections, or GUI elements), can’t be directly represented in JSON. You’d need to serialize those into simpler structures (e.g., strings or arrays). - Circular References.
JSON can’t handle circular references, where an object refers to itself, directly. For example, if Object A has a reference to Object B, and Object B has a reference back to Object A, this would cause an infinite loop when trying to serialize it.
Since JSON only supports strings, numbers, objects, arrays, booleans, and null, developers turn to other data formats or techniques that allow for richer data structures. The following are some alternatives and approaches that can help when JSON’s simple data types just aren’t enough.
1. JSON5
- JSON5 is an extension of JSON that supports comments, allows trailing commas, and handles more flexible syntax (like unquoted object keys and single-quoted strings). It’s not necessarily more efficient, but it’s easier for developers to work with and debug.
- If you find yourself frustrated by the strict syntax rules of JSON (like no comments or trailing commas), JSON5 is a better fit. It’s super compatible with existing JSON tools and libraries but gives you more developer-friendly features.
2. BSON (Binary JSON)
- BSON is a binary version of JSON, developed for use with MongoDB. It supports more data types than JSON, including binary data, dates, and embedded documents. It’s more compact and efficient for data storage and transmission.
- If you’re working with MongoDB or need a binary format that supports richer data types and more efficient storage/transfer, BSON is the way to go. It solves the problem of needing to serialize things like dates and binary objects that JSON can’t handle natively.
3. MessagePack
- MessagePack is a binary serialization format that’s more compact than JSON, but still retains compatibility with JSON data structures. It can serialize data in a smaller size and faster than JSON, making it great for low-bandwidth or high-performance applications.
- If you need to send large amounts of data over networks and want to reduce latency and size, MessagePack is a great replacement. It’s increasingly being adopted in IoT, mobile apps, and gaming for its speed and efficiency.
4. CBOR (Concise Binary Object Representation)
- CBOR is a compact binary data format designed for the Internet of Things (IoT) and other environments where efficiency matters. It’s similar to JSON but more compact and can handle richer data types like binary data, timestamps, and more complex structures.
- CBOR is designed to work well with low-powered devices (think IoT) while still being able to serialize complex data more efficiently than JSON. If you need a binary format that’s more compact but still flexible, CBOR is a solid choice.
5. Protobuf (Protocol Buffers)
- Protobuf is a binary format developed by Google that allows for efficient, compact serialization of structured data. It’s not human-readable (like JSON), but it’s smaller and faster and supports complex data structures with strong typing (e.g., enums, nested messages).
- If you’re dealing with microservices or need a highly efficient and type-safe data exchange, Protobuf is fantastic. It’s much faster and more compact than JSON, making it ideal for high-performance systems.
6. Avro
- Avro is a compact, fast serialization format, popular in big data applications (like those using Apache Kafka or Hadoop). It supports complex data types, including schemas, so you can version your data and ensure consistency over time.
- If you’re working in a distributed data system or need schema validation and data evolution features, Avro is better than JSON for maintaining data integrity across versions and services. It’s also more efficient for big data workloads.
7. FlatBuffers
- FlatBuffers is a serialization format developed by Google, designed for speed and memory efficiency. It’s similar to Protobuf but faster to serialize and deserialize because it doesn’t require parsing or copying, just direct access to the data.
- If you need low-latency access to your data, like in games or mobile apps, FlatBuffers is better suited for environments where you need quick serialization and access without the overhead of converting data into a usable format.
8. ORC (Optimized Row Columnar)
- ORC is a highly optimized columnar storage format used in big data systems (mainly with Apache Hive). It’s designed to efficiently store and process large datasets by storing data in columns rather than rows, leading to improved compression and query performance.
- ORC is particularly useful for big data storage where you need fast querying and high compression. It’s not a direct replacement for JSON but is a much better choice in data-heavy environments.
9. YAML (YAML Ain’t Markup Language)
- YAML is a human-readable data serialization format often used for configuration files, data exchange, and even APIs. While it’s more flexible than JSON and allows for complex structures, it’s easier to read and write, especially for configuration purposes.
- YAML is better if you’re dealing with configuration files or want something that’s easy to edit by humans. It can represent more complex structures than JSON, and its syntax is generally more concise and user-friendly for developers.