Skip to content

Advanced Patterns

JSSON isn’t just for simple key-value pairs. Its features can be combined to solve complex configuration challenges. Here are some advanced patterns to supercharge your workflow.

Avoid repetition and magic numbers by using variables (:=). Variables are scoped and don’t appear in the final output.

// Define constants
base_url := "https://api.example.com"
timeout := 5000
retries := 3
// Environment configuration
production {
url = base_url
timeout = timeout
retry_count = retries
}
staging {
url = "https://staging.example.com"
timeout = timeout
retry_count = retries
}

Need to generate thousands of records for load testing? JSSON’s ranges and string concatenation make it trivial.

Combine numeric ranges with string concatenation to generate calendar dates:

test_data [
template { date, event }
map (row) = {
date = "2025-01-" + row.day
event = "Login Attempt"
status = "Success"
}
// Generate dates from 1 to 31
1..31
]

Create realistic user profiles by zipping ranges and using templates:

users [
template { id, department }
map (u) = {
id = u.id
username = "user_" + u.id
email = "user_" + u.id + "@company.com"
dept = u.department
role = "employee"
}
// Generate 5 users for Engineering
100..104, Engineering
// Generate 5 users for Sales
200..204, Sales
]

Manage multiple environments (Dev, Staging, Prod) cleanly using the Base + Overlay pattern with include.

Define the common settings shared across all environments.

base.jsson
app {
name = "MyApp"
version = "1.0.0"
retry_attempts = 3
}
database {
driver = "postgres"
pool_size = 10
}

Include the base config and override only what changes.

prod.jsson
include "base.jsson"
// Override database settings for production
database {
host = "prod-db.aws.com"
pool_size = 100 // Higher pool size for prod
}
// Add prod-specific settings
logging {
level = "error"
format = "json"
}

Why this works: JSSON’s include merges the included file into the current scope. If you redefine a key (like database), JSSON merges the new properties into the existing object, allowing for granular overrides.

Enforce naming conventions automatically using the map clause. This ensures consistency across your infrastructure configuration.

resources [
template { name, type, region }
map (res) = {
// Auto-generate standardized ID: type-name-region
id = res.type + "-" + res.name + "-" + res.region
// Pass through original fields
name = res.name
type = res.type
region = res.region
// Add default tags
tags {
managed_by = "jsson"
env = "production"
}
}
"web-server", ec2, "us-east-1"
"db-primary", rds, "us-west-2"
"cache-cluster", redis, "eu-central-1"
]

Since v0.0.5, JSSON supports nested map() transformations, making matrix generation trivial!

// Generate a 3x3 grid using nested maps
grid = (1..3 map (y) = (1..3 map (x) = {
x = x
y = y
}))

Generate a multiplication table or any numeric matrix:

// Multiplication table
multiplicationTable = (1..5 map (row) = (1..5 map (col) = row * col))
// Simple numeric grid
matrix = (0..2 map (row) = (0..2 map (col) = row * 3 + col))

Create all combinations of product variants:

// All size/color combinations
products = (["S", "M", "L", "XL"] map (size) = (
["red", "blue", "green", "black"] map (color) = {
size = size
color = color
sku = size + "-" + color
price = 29.99
}
))

Generate a complete chess board with coordinates:

chessBoard = (["a", "b", "c", "d", "e", "f", "g", "h"] map (file) = (
1..8 map (rank) = {
square = file + rank
file = file
rank = rank
color = (rank % 2) == 0 ? "white" : "black"
}
))

Pro Tip: Nested maps unlock infinite possibilities for matrix-like data structures. Combine with ranges for explosive data generation!