Representing logic as data in JSON
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Representing logic as data in JSON means encoding conditional rules, boolean expressions, and decision trees as JSON objects instead of hard-coding them in application code. This pattern is used in rule engines, form builders, access control systems, and workflow automation tools. The core idea is to define operators and conditions as nested JSON structures that an interpreter evaluates at runtime, making business logic configurable without code deployments.
Basic Condition Objects
The simplest approach represents each condition as a JSON object with a field, an operator, and a value:
An interpreter evaluates this against a data context:
Boolean Combinators (AND, OR, NOT)
Combine conditions with logical operators using nested structures:
The evaluator handles combinators recursively:
This recursive structure can represent any boolean expression.
If-Then Rules
Add actions to conditions to build a rule engine:
Decision Trees
Represent branching logic as nested if-else structures:
Existing Standards and Libraries
Several established formats encode logic as JSON:
JsonLogic is a portable standard with implementations in JavaScript, Python, PHP, Ruby, and more. It supports arithmetic, string operations, array operations, and custom operators.
Access Control Example
Storing access rules as JSON allows non-developers to modify permissions through an admin UI without changing application code.
Common Pitfalls
- Unbounded recursion depth: Deeply nested AND/OR/NOT structures can overflow the call stack. Set a maximum depth limit in your evaluator (e.g., 10 levels) and reject rules that exceed it.
- No schema validation: Without validation, malformed rules silently produce wrong results. Define a JSON Schema for your rule format and validate all rules before storing or evaluating them.
- Security risks with user-defined rules: If users can define rules, they might craft expensive conditions (like regex matching on large strings). Sanitize inputs and set evaluation timeouts.
- Missing operator coverage: When you add a new field type but forget to add its operators, conditions silently fail. Throw errors for unknown operators rather than returning false.
- Performance with large rule sets: Evaluating hundreds of rules per request is slow. Index rules by relevant fields, short-circuit evaluation, and cache results for identical data contexts.
Summary
- Represent conditions as
{field, operator, value}objects evaluated by an interpreter - Combine conditions with
and,or, andnotwrappers for arbitrary boolean logic - Add actions to conditions for rule engines, or nest branches for decision trees
- Use established standards like JsonLogic for portability across languages
- Validate rule schemas and limit recursion depth to prevent malformed or malicious rules
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.