How to Generate Multivariate JSON Mock Data in Python
This script generates mock multivariate JSON-compatible data with measurements and boolean flags for testing and experimentation pipelines.
Python code
26 linesimport json
def multivariate_mock(row_count: int = 3) -> list:
"""Generate mock multivariate data as list of JSON-compatible dicts."""
records = []
for i in range(row_count):
record = {
"id": i + 1,
"measurements": {
"temperature": 20.5 + i * 1.5,
"pressure": 1013.2 - i * 2.1,
"vibration_level": 0.02 + i * 0.01
},
"status": "normal",
"flags": {
"is_active": True,
"is_critical": (i == 2),
"system_online": i % 2 == 0
}
}
records.append(record)
return records
if __name__ == "__main__":
result = multivariate_mock()
print(json.dumps(result, indent=2))
Output
[
{
"id": 1,
"measurements": {
"temperature": 20.5,
"pressure": 1013.2,
"vibration_level": 0.02
},
"status": "normal",
"flags": {
"is_active": true,
"is_critical": false,
"system_online": true
}
},
{
"id": 2,
"measurements": {
"temperature": 22.0,
"pressure": 1011.1,
"vibration_level": 0.03
},
"status": "normal",
"flags": {
"is_active": true,
"is_critical": false,
"system_online": false
}
},
{
"id": 3,
"measurements": {
"temperature": 23.5,
"pressure": 1009.0,
"vibration_level": 0.04
},
"status": "normal",
"flags": {
"is_active": true,
"is_critical": true,
"system_online": true
}
}
]
How it works
The multivariate_mock function builds a list of dictionaries with realistic measurement values that scale linearly with the row index. Each record contains nested structures for measurements and boolean flags, making it JSON-compatible out of the box. The json.dumps call with indent=2 formats the output for readable inspection. Toggling row_count lets you control how many mock records you generate.
Common mistakes
- Forgetting to pass `row_count` when you need more than 3 rows
- Assuming the boolean flag patterns are random instead of deterministic
- Not using `if __name__ == '__main__'` so the function runs on import
Variations
- Use `random` module to add noise or variability to measurement values
- Return a list of dicts directly without json.dumps for use in other Python code
Real-world use cases
- Feed synthetic multivariate data into an A/B test evaluation system to validate metric aggregations.
- Populate dashboards or alerts with realistic mock sensor readings before hardware is available.
- Generate baseline records for staging environments when building experiment bucketing and feature-flag logic.
Sponsored
More from A/B testing & experimentation
Keep learning
Related tutorials and quizzes for this topic.