JSON Basics for Template Authors¶
JSON (JavaScript Object Notation) is a simple way to store and organize data in a text file. Don't let the technical-sounding name intimidate you - it's actually quite straightforward once you understand the basics!
Why JSON?¶
Templify uses JSON to provide the data that fills in your template placeholders. Think of JSON as a way to write down information in a structured format that computers can easily read.
The Five Types of Data in JSON¶
1. Text (Strings)¶
Text values must be wrapped in double quotes:
Rules for text:
- Always use double quotes (") not single quotes (')
- To include a quote inside text, use \": "She said \"Hello\""
- To include a backslash, use \\: "C:\\Users\\Documents"
2. Numbers¶
Numbers don't need quotes:
Rules for numbers:
- No quotes around numbers
- Use a dot (.) for decimals, not a comma
- Negative numbers start with -
3. True/False (Booleans)¶
For yes/no values, use true or false (no quotes, all lowercase):
Rules for true/false:
- Must be lowercase: true or false
- No quotes around them
- These are perfect for conditionals in your templates
4. Lists (Arrays)¶
Lists let you have multiple values. They're wrapped in square brackets [ ]:
{
"Colors": ["Red", "Green", "Blue"],
"Prices": [10.99, 25.00, 15.50],
"Tags": ["new", "featured", "sale"]
}
Rules for lists:
- Wrap the list in square brackets: [ ]
- Separate items with commas
- All items should be the same type (all text, all numbers, etc.)
- Can be empty: []
5. Objects (Nested Data)¶
Objects let you group related data together. They're wrapped in curly braces { }:
Rules for objects:
- Wrap in curly braces: { }
- Each piece of data is "key": value
- Separate pieces with commas
- Can contain any type of data, including other objects and lists
Complete JSON File Structure¶
Every JSON file for Templify follows this pattern:
Key rules:
1. Start with { and end with } - These wrap everything
2. Each line has "Name": value - The name (key) and its value
3. Separate lines with commas - But NOT after the last line
4. Names (keys) must have double quotes - Values depend on the type
Common Examples¶
Simple Contact Information¶
{
"FirstName": "Sarah",
"LastName": "Connor",
"Phone": "+1-555-0123",
"Age": 28,
"IsSubscribed": true
}
Nested Information (Objects Within Objects)¶
{
"Company": {
"Name": "Acme Corp",
"Founded": 1995,
"Address": {
"Street": "123 Main St",
"City": "Springfield",
"Country": "USA"
}
}
}
In your template, access nested data with dots:
- {{Company.Name}} → "Acme Corp"
- {{Company.Address.City}} → "Springfield"
Lists of Items¶
{
"CustomerName": "John Doe",
"OrderItems": [
{
"ProductName": "Widget",
"Quantity": 2,
"Price": 10.00
},
{
"ProductName": "Gadget",
"Quantity": 1,
"Price": 25.00
}
]
}
In your template, use loops:
Combining Everything¶
{
"CustomerName": "Alice Johnson",
"IsVIP": true,
"TotalSpent": 1250.50,
"RecentOrders": [
{
"OrderDate": "2024-01-15",
"Amount": 50.00,
"Status": "Delivered"
},
{
"OrderDate": "2024-02-20",
"Amount": 75.00,
"Status": "Shipped"
}
],
"PreferredContact": {
"Method": "Email",
"Value": "alice@example.com",
"SendPromotions": true
}
}
Common Mistakes and How to Fix Them¶
❌ Missing Comma¶
Wrong:
Right:
❌ Extra Comma at the End¶
Wrong:
Right:
❌ Missing Quotes Around Keys¶
Wrong:
Right:
❌ Single Quotes Instead of Double Quotes¶
Wrong:
Right:
❌ Numbers in Quotes¶
If you put numbers in quotes, they become text (usually fine, but can cause issues with comparisons):
Less than ideal:
Better:
❌ Missing Closing Bracket or Brace¶
Wrong:
Right:
Practical Tips¶
Start Small¶
Begin with simple data and gradually add complexity:
Step 1: Simple
Step 2: Add More
Step 3: Add Nesting
Use a JSON Validator¶
Before using your JSON file with Templify, validate it:
- Go to jsonlint.com
- Paste your JSON
- Click "Validate JSON"
- Fix any errors it reports
Use a Good Text Editor¶
Some text editors help you write JSON:
- VS Code - Free, shows errors as you type, auto-indents
- Notepad++ - Free, syntax highlighting
- Sublime Text - Free trial, clean interface
Avoid Microsoft Word or rich-text editors - they add invisible formatting that breaks JSON!
Format for Readability¶
Hard to read:
Easy to read:
Most text editors can auto-format JSON for you (often with Shift+Alt+F or a "Format Document" command).
How JSON Maps to Template Placeholders¶
The structure of your JSON determines how you write placeholders:
Simple Fields¶
JSON:
Template:
Nested Fields¶
JSON:
Template:
Lists/Arrays¶
JSON:
Template:
Array Item by Index¶
JSON:
Template:
Quick Reference¶
| Data Type | JSON Example | Template Example |
|---|---|---|
| Text | "Name": "Alice" |
{{Name}} |
| Number | "Age": 25 |
{{Age}} |
| True/False | "IsActive": true |
{{#if IsActive}}...{{/if}} |
| Nested Object | "Customer": { "Name": "Alice" } |
{{Customer.Name}} |
| List | "Items": ["A", "B"] |
{{#foreach Items}}{{.}}{{/foreach}} |
Next Steps¶
Now that you understand JSON basics, learn how to use it in your templates:
- Placeholders - Using data in your templates
- Conditionals - Showing content based on data values
- Loops - Repeating content for lists
- Getting Started - Complete beginner tutorial
Need Help?¶
If you're stuck:
- Validate your JSON at jsonlint.com
- Check for common mistakes (missing commas, quotes, brackets)
- Start simple and add complexity gradually
- Look at examples in our Examples Gallery
Remember: Everyone makes JSON mistakes at first. Use a validator, and you'll get the hang of it quickly!