Overview
AsTaJson serializes a single model object into a JSON string, using a ViewModel as the structural template that defines which attributes to include and how to name them. The operator is called on any single object — not a collection — and requires a rooted ViewModel to act as the data-shape definition.
It is MDriven's way of letting you take full control over the JSON your application produces, rather than relying on the default MDriven REST serialization format.
Syntax
<object>.AsTaJson( viewModelName, skipEmpties )
Important: AsTaJson is called on a single object, not a collection. If you need to serialize many objects, you must first establish a single root object that holds the collection see the Handling Multiple Objects section below.
Parameters
| Parameter | Type | Description |
| <object> | Single instance | The model object to serialize. Must be a single instance, not a collection (->). |
| viewModelName | ViewModel | The ViewModel used as the JSON structure template. Accessed via <Class>.ViewModels.<ViewModelName>. The ViewModel can be rooted, and this object is passed in as its root. |
| skipEmpties | Boolean | Controls whether attributes with null or empty values are included in the JSON output. See details below. |
The skipEmpties Parameter
Some attributes in your ViewModel template may resolve to null or empty values for a given object. The skipEmpties flag controls how AsTaJson handles these:
false — Include all attributes in the JSON output, even if their value is null or empty. The key will appear in the output with a null or empty value.
true — Omit any attribute whose value is null or empty. Those keys will not appear in the JSON output at all.
Return Value
Type: String — A JSON-encoded string. In the context of a REST endpoint, assign this to a column named RoJson to override MDriven's default serialization and return your own custom JSON structure.
How It Works
When AsTaJson is called on an object, it passes that object in as the root of the specified ViewModel. The ViewModel then resolves all its column expressions (self.author, self.title, etc.) against that object, and the resulting values are serialized into JSON. The column names in the ViewModel become the JSON keys.
This means:
• The ViewModel is purely a template — a shape definition. It does not need its own UI.
• Sub-ViewModels (grids/lists) within the template produce nested JSON arrays.
• You control the JSON key names by choosing the column names in the template ViewModel.
Example 1: Single Object
When your data is a single known object, you can call AsTaJson directly on it without any extra scaffolding. This is the simplest case.
Scenario
You want to return one article as JSON from a REST endpoint. Your ViewModel template (ArticlesOneTemplate) is backed by the Articles1 class and has columns for description, title, and author.
Expression on the RawJson column
vSeekerResult->first.AsTaJson( Articles1.ViewModels.ArticlesOneTemplate, false )
vSeekerResult->first gets a single object from the result collection. AsTaJson is then called on that single object, passing the template ViewModel. Because the ViewModel is rooted, this object becomes its root, and the column expressions resolve against it.
Expected JSON output:
{
"author": "Tim De Chant",
"title": "Lunar Energy raises $232M to deploy home batteries",
"description": "The startup has raised more than $500 million..."
}
Example 2: Multiple Objects (List of Results)
AsTaJson requires a single object as its receiver. When you have a collection and want to return all of it as JSON, you need to create a root object that holds the collection. This is the most common pattern when building API endpoints.
| Why? A collection (->collect, ->select, etc.) cannot be the receiver of AsTaJson. There is no single entry point for the JSON tree. You need one object where the serialization can start a root and that root then navigates to the collection. |
The Pattern: Use a Singleton as the Root
The standard approach is to use a SysSingleton (or any class with a single persistent instance) as the root object. You attach the result collection to it via a transient association (persistence = false), so the data is available during the request but never permanently saved.
Step 1 — Create a transient association on SysSingleton
Add an association from SysSingleton to Articles1 named ApiSearchResults. Set the association's persistence to false. This means objects added to it exist only for the lifetime of the session — they are never written to the database.
| Why transient? You are not storing articles on SysSingleton permanently. The association is only a staging area a temporary holding list used to bridge the search results into the JSON template during serialization. |
Step 2 — Create the JSON template ViewModel (ArticlesJSONTemplate)
Create a ViewModel backed by SysSingleton with Requires Root Object checked. Add a sub-ViewModel (grid) called Results or Articles, backed by Articles1, mapped to self.ApiSearchResults. Add columns for the fields you want in your JSON output:
The column names become your JSON keys. Rename them here to control what the API consumer sees.
Step 3 — Populate ApiSearchResults in the Seeker ViewModel
In your ArticlesSeekerViewModel, add a button (or action) that runs after the search. Buttons on server-side ViewModels and REST endpoints are automatically executed on each request. Inside that button:
SysSingleton.oclSingleton.ApiSearchResults->clear; vSeekerResult->collect(r|SysSingleton.oclSingleton.ApiSearchResults->add(r))
Always clear first. Without clearing, cached results from a previous search will accumulate and pollute the next response.
Step 4 — Call AsTaJson on the singleton
In the ArticlesSeekerViewModel, add a column named RawJson. This special column name tells MDriven to resturn all these values defined in here. The expression:
SysSingleton.oclSingleton.AsTaJson(SysSingleton.ViewModels.ArticlesJSONTemplate,false)
SysSingleton.oclSingleton gets the single SysSingleton instance. .AsTaJson(...) called on that single object. SysSingleton.ViewModels.ArticlesJSONTemplate the template ViewModel. false include all attributes even if empty.
The resulting JSON
With the sub-ViewModel named Articles and columns as defined above, the output looks like:
The REST endpoint is then accessible at:
GET localhost:8182/Rest/ArticlesSeekerViewModel/Get
RawJson Returning Custom JSON from a REST Endpoint
When a ViewModel is exposed as a REST endpoint (Eco.RestAllowed: True), MDriven by default serializes whatever data is in the ViewModel using its own format. To override this and return your own JSON:
•Add a column named exactly RawJson to the ViewModel.
• Set its expression to your AsTaJson call.
• MDriven will use the string returned by that column as the entire HTTP response body.
See Tajson for more information.
