JSON (JavaScript Object Notation) Notes
JSON (JavaScript Object Notation)
Overview
- JSON is a data format used in AWS, particularly for policy documents and CloudFormation.
- It's an alternative to YAML, which is primarily used for CloudFormation.
- Many concepts from YAML apply to JSON, but the format differs.
Definition
- 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.
Key Differences from YAML
- Indentation: JSON is less strict about indentation due to its enclosure-based structure (braces and brackets).
- Readability: Initially, JSON may seem harder to read, but with experience, its structure becomes clear.
Main Elements of JSON
1. JSON Object
- An unordered set of key-value pairs enclosed in curly brackets
{}. - Equivalent to a "dictionary" in YAML.
2. Array
- An ordered collection of values separated by commas and enclosed in square brackets
[]. - Equivalent to a "list" in YAML.
Values
- Values in arrays or objects can be:
- String
- Object
- Number
- Array
- Boolean (
true or false) null
Example of a Simple JSON Document
{
"cats": ["Whiskers", "Mittens", "Fluffy"],
"colors": ["Gray", "White", "Black"],
"num_of_eyes": [2, 2, 2]
}
- This example demonstrates a top-level JSON object with three keys:
cats, colors, and num_of_eyes. - Each key has an array as its value.
Complex JSON Structures
- JSON documents can be nested.
- Objects can contain other objects or arrays.
- Arrays can contain objects.
- This nesting allows for complex data structures.
Example
{
"Ruffle": {
"age": 3,
"color": "white"
},
"Truffles": {
"age": 5,
"color": "black"
},
"Penny": {
"age": 1,
"color": "gray"
},
"Winky": {
"age": 2,
"color": "tabby"
}
}
Personal Preference
- JSON is a matter of personal preference.
- For AWS, JSON has more utility because it is used for both CloudFormation and identity policy documents.
- YAML is currently only used for CloudFormation.
- This example creates an S3 bucket.
{
"Resources": {
"S3Bucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "my-unique-bucket-name"
}
}
}
}
- Structure:
Resources key with a JSON object as its value.S3Bucket key within Resources with another JSON object as its value.Type key defines the resource type.Properties key contains a JSON object with the BucketName.
Indentation in JSON
- Indentation is not formally required in JSON.
- JSON relies on curly and square brackets for structure.
- However, indentation improves readability.
Practical Experience
- The course will provide opportunities to work with JSON and YAML.
- Emphasis on understanding and reading YAML and JSON before writing.
- CloudFormation mini deep dive lessons will cover writing scalable and portable CloudFormation templates using YAML and JSON.