Jsson Docs
Guides

Advanced Patterns

Unlock the full potential of JSSON with these powerful configuration 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.

Clean Configuration with Variables

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
}
{
  "production": {
    "url": "https://api.example.com",
    "timeout": 5000,
    "retry_count": 3
  },
  "staging": {
    "url": "https://staging.example.com",
    "timeout": 5000,
    "retry_count": 3
  }
}

🧪 Test Data Generation

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

Generating Dates

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
]
{
  "test_data": [
    {
      "date": "2025-01-1",
      "event": "Login Attempt",
      "status": "Success"
    },
    // ... up to 31
    {
      "date": "2025-01-31",
      "event": "Login Attempt",
      "status": "Success"
    }
  ]
}

Generating User Accounts

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
]
{
  "users": [
    {
      "id": 100,
      "username": "user_100",
      "email": "user_100@company.com",
      "dept": "Engineering",
      "role": "employee"
    },
    // ...
    {
      "id": 204,
      "username": "user_204",
      "email": "user_204@company.com",
      "dept": "Sales",
      "role": "employee"
    }
  ]
}

🌍 Environment Management

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

1. Base Configuration (base.jsson)

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
}

2. Environment Overlays (prod.jsson)

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.

🏷️ Dynamic Resource Naming

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"
]
{
  "resources": [
    {
      "id": "ec2-web-server-us-east-1",
      "name": "web-server",
      "type": "ec2",
      "region": "us-east-1",
      "tags": { "managed_by": "jsson", "env": "production" }
    },
    {
      "id": "rds-db-primary-us-west-2",
      "name": "db-primary",
      "type": "rds",
      "region": "us-west-2",
      "tags": { "managed_by": "jsson", "env": "production" }
    },
    {
      "id": "redis-cache-cluster-eu-central-1",
      "name": "cache-cluster",
      "type": "redis",
      "region": "eu-central-1",
      "tags": { "managed_by": "jsson", "env": "production" }
    }
  ]
}

🔢 Matrix Generation

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

Simple Coordinate Grid

// Generate a 3x3 grid using nested maps
grid = (1..3 map (y) = (1..3 map (x) = {
  x = x
  y = y
}))
{
  "grid": [
    [
      { "x": 1, "y": 1 },
      { "x": 2, "y": 1 },
      { "x": 3, "y": 1 }
    ],
    [
      { "x": 1, "y": 2 },
      { "x": 2, "y": 2 },
      { "x": 3, "y": 2 }
    ],
    [
      { "x": 1, "y": 3 },
      { "x": 2, "y": 3 },
      { "x": 3, "y": 3 }
    ]
  ]
}

Numeric Matrix

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))
{
  "multiplicationTable": [
    [1, 2, 3, 4, 5],
    [2, 4, 6, 8, 10],
    [3, 6, 9, 12, 15],
    [4, 8, 12, 16, 20],
    [5, 10, 15, 20, 25]
  ],
  "matrix": [
    [0, 1, 2],
    [3, 4, 5],
    [6, 7, 8]
  ]
}

Combinatorial Product Catalog

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
  }
))
{
  "products": [
    [
      { "size": "S", "color": "red", "sku": "S-red", "price": 29.99 },
      { "size": "S", "color": "blue", "sku": "S-blue", "price": 29.99 },
      { "size": "S", "color": "green", "sku": "S-green", "price": 29.99 },
      { "size": "S", "color": "black", "sku": "S-black", "price": 29.99 }
    ],
    [
      { "size": "M", "color": "red", "sku": "M-red", "price": 29.99 }
      // ... and so on for M, L, XL
    ]
  ]
}

Advanced: Chess Board

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!

On this page