[Avg. reading time: 5 minutes]
JSON
JSON (JavaScript Object Notation) is a text-based format used to store and exchange structured data.
It is widely used across APIs, files, and data pipelines.
Simplest JSON format
{"id": "1","name":"Rachel"}
Properties
- Language-independent
- Human-readable when keys are well named
- Lightweight and easy to parse
- Widely supported across systems and tools
Basic Rules
- Curly braces { } represent an object
- Data is represented as key-value pairs
- Keys and string values must use double quotes
- Pairs are separated by commas
- Square brackets [ ] represent an array
JSON Values
String {"name":"Rachel"}
Number {"id":101}
Boolean {"result":true, "status":false} (lowercase)
Object {
"character":{"fname":"Rachel","lname":"Green"}
}
Array {
"characters":["Rachel","Ross","Joey","Chanlder"]
}
NULL {"id":null}
Sample JSON Document
{
"characters": [
{
"id" : 1,
"fName":"Rachel",
"lName":"Green",
"status":true
},
{
"id" : 2,
"fName":"Ross",
"lName":"Geller",
"status":true
},
{
"id" : 3,
"fName":"Chandler",
"lName":"Bing",
"status":true
},
{
"id" : 4,
"fName":"Phebe",
"lName":"Buffay",
"status":false
}
]
}
JSON Best Practices
Avoid hyphens in keys
{"first-name":"Rachel","last-name":"Green"}
- Valid JSON
- But awkward to access in many programming languages
snake_case
{"first_name":"Rachel","last_name":"Green"}
Lowercase
{"firstname":"Rachel","lastname":"Green"}
Camelcase (Common in APIs)
{"firstName":"Rachel","lastName":"Green"}
Pick one style and stay consistent
JSON & RUST
- The Deserialize trait is required to parse (that is, read) JSON strings into this Struct.
- The Serialize trait is required to format (that is, write) this Struct into a JSON string.
- The Debug trait is for printing a Struct on a debug trace.
git clone https://github.com/gchandra10/rust_json_serialize_deserialize.git