aiPopulate
Populate a class instance, struct, or array from JSON data or struct. This is useful for working with structured AI output, testing, custom workflows, or deserializing cached AI responses.
Syntax
aiPopulate(target, data)Parameters
target
any
Yes
The target to populate - class instance, array with single instance [new MyClass()], or struct template
data
any
Yes
The data to populate from - JSON string or struct
Target Types
Class Instance:
new Employee()- Populates propertiesArray:
[new Employee()]- Populates array of instancesStruct:
{ name: "", age: 0 }- Returns deserialized data (validation template)
Returns
Returns the populated target:
Class Instance: The same instance with populated properties
Array: Array of populated class instances
Struct: Deserialized data struct
Examples
Basic Class Population
From Struct
Array Population
With AI Response
Cached Response Population
Testing Without AI
Struct Validation Template
Complex Nested Objects
API Response Mapping
Batch Processing
Error Handling
With Default Values
Dynamic Class Population
Workflow Integration
Notes
🎯 Type Safety: Maps JSON/struct data to typed class properties
🔄 Reusability: Populate same class from different data sources
🧪 Testing: Test AI workflows without actual API calls
💾 Caching: Deserialize cached AI responses efficiently
📦 Flexibility: Works with classes, arrays, and structs
⚡ Performance: No reflection overhead - direct property setting
🔧 Integration: Seamlessly integrates with structured output workflows
Related Functions
aiChat()- Get structured AI responsesaiService()- Direct service invocationaiMessage()- Build structured prompts
Best Practices
✅ Use for structured output - Pair with response_format: { type: "json_object" }
✅ Test without AI - Use mock data for development and testing
✅ Cache expensive calls - Populate from cached JSON to avoid API costs
✅ Define clear classes - Use typed properties for validation
✅ Handle arrays properly - Wrap class instance in array: [new Class()]
✅ Validate input - Wrap in try/catch for production robustness
❌ Don't skip validation - Always validate JSON structure before populating
❌ Don't ignore defaults - Class property defaults are preserved
❌ Don't assume structure - Check AI response format before populating
Last updated