You can use AsTaJson when you need an OCL expression to serialize one model object into JSON shaped by a ViewModel, including nested objects and lists; it is intended for developers defining JSON output such as a custom REST response.
Syntax
<object>.AsTaJson(<viewModel>, <skipEmpties>)The receiver, <object>, must be one object. The <viewModel> argument identifies the rooted ViewModel that defines the JSON structure. The result is a JSON-formatted string.
What AsTaJson does
AsTaJson uses a ViewModel as a JSON template:
- The object before
.AsTaJsonbecomes the root object used by the ViewModel. - ViewModel column expressions are evaluated against that root object.
- ViewModel column names become JSON property names.
- A nested ViewModel for a many association produces a JSON array.
- A nested ViewModel for a single association produces a nested JSON object according to the ViewModel structure.
For example, if a ViewModel rooted in Articles1 has columns named author, title, and description, expressions such as self.author and self.title provide the values. Calling AsTaJson for one article can produce:
{
"author": "Tim De Chant",
"title": "Lunar Energy raises $232M to deploy home batteries",
"description": "The startup has raised more than $500 million..."
}
The template ViewModel defines output shape. It does not need to be used as a user interface.
Parameters
| Parameter | Purpose |
|---|---|
<object>
|
The single model object to serialize. It is the root object for the template ViewModel. |
<viewModel>
|
A rooted ViewModel that selects the attributes, nested ViewModels, expressions, and output property names. |
<skipEmpties>
|
A Boolean value that controls whether empty or null values are emitted. |
skipEmpties
| Value | Result |
|---|---|
false
|
Include properties even when their values are null or empty. The property remains in the JSON output. |
true
|
Omit properties whose values are null or empty. The property is not present in the JSON output. |
Choose the value required by the receiving JSON contract. For example, with skipEmpties set to false, a missing article description can remain visible as a description property. With true, that property is omitted.
Return value
AsTaJson returns a String containing JSON text.
vSeekerResult->first.AsTaJson(Articles1.ViewModels.ArticlesOneTemplate, false)In this example, vSeekerResult is a collection, but ->first selects one Articles1 object before AsTaJson is called. The ArticlesOneTemplate ViewModel is then evaluated with that article as its root.
Create JSON for one object
Use this pattern when your expression already identifies one object.
- Create a ViewModel rooted in the class that you will serialize. For example, root
ArticlesOneTemplateinArticles1. - Configure the ViewModel as requiring a root object.
- Add columns for the properties that the JSON must contain. Name each column after the desired JSON property and give it an expression, such as
self.title. - Call AsTaJson on one
Articles1object.
vSeekerResult->first.AsTaJson(Articles1.ViewModels.ArticlesOneTemplate, false)Do not call AsTaJson directly on vSeekerResult, because that expression is a collection rather than one root object.
Create JSON for a collection
AsTaJson has one root-object receiver. To return a list of result objects, create one root object that temporarily holds the collection, then serialize that root.
A common implementation uses SysSingleton as the root and a non-persistent association to hold request results.
Example: return search results under an Articles property
Assume a search produces vSeekerResult, a collection of Articles1 objects. The desired JSON has a root object with an Articles array.
- Add an association from
SysSingletontoArticles1, namedApiSearchResults. - Set the association persistence to
false. The association holds transient results and is not written to the database. - Create a ViewModel rooted in
SysSingleton, for exampleArticlesJSONTemplate, and configure it to require a root object. - Add a nested ViewModel named
Articles, backed byArticles1and mapped toself.ApiSearchResults. - In the nested ViewModel, add and name columns for the properties to expose, such as
author,title, anddescription. - Before generating JSON, clear the transient association and add the current search results.
SysSingleton.oclSingleton.ApiSearchResults->clear;
vSeekerResult->collect(r | SysSingleton.oclSingleton.ApiSearchResults->add(r))Clearing the association is required. If you do not clear it, results retained from an earlier request can be included in the next response.
Call AsTaJson on the singleton, which is one object:
SysSingleton.oclSingleton.AsTaJson(
SysSingleton.ViewModels.ArticlesJSONTemplate,
false
)The nested ViewModel creates an array similar to:
{
"Articles": [
{
"author": "Tim De Chant",
"title": "Lunar Energy raises $232M to deploy home batteries",
"description": "The startup has raised more than $500 million..."
}
]
}
Return custom JSON from a REST ViewModel
When a ViewModel is exposed as a REST endpoint, you can use a column named exactly RawJson to supply the response JSON rather than the default ViewModel serialization.
- Add a column named
RawJsonto the REST ViewModel. - Set its expression to an AsTaJson call that returns the required JSON string.
- Ensure the expression is evaluated after any action that populates the root object's transient result association.
For the collection pattern, the RawJson expression is:
SysSingleton.oclSingleton.AsTaJson(
SysSingleton.ViewModels.ArticlesJSONTemplate,
false
)Use RawJson only when you need to control the complete response structure. For the broader declarative JSON import, update, transformation, and storage features, see Documentation:Tajson.
Control special JSON structures
Set the following tagged values to true on a multi-nesting column in the template ViewModel when the target JSON contract needs a structure that a standard nested list does not produce.
| Tagged value | Use | Example result |
|---|---|---|
TaJsonTreatListAsDynamicProperties
|
Treat each object in a nested list as a runtime-defined property. AsTaJson reads Name and Value properties from each list object and uses them as JSON property names and values.
|
A list containing Name = "priority" and Value = "high" can become "priority": "high".
|
TaJsonTreatListAsValues
|
Generate an array from the first column of the nested ViewModel instead of an array of JSON objects. | A standard list can produce "someList": [{"col1":"value1"}, {"col1":"value2"}]; with this tagged value, it can produce "someList": ["value1", "value2"].
|
RawJSon
|
Insert already formatted JSON from a string value without serializing it as a quoted JSON string. | A column containing {"enabled":true} is inserted as JSON content, not as the string "{\"enabled\":true}".
|
Use RawJSon only when the supplied value is valid JSON. Invalid JSON makes the generated result invalid.
Naming JSON properties
Name columns in the template ViewModel for the JSON property names required by the consumer. For example, a column named title with expression self.Title produces the JSON property "title", while the underlying model attribute can retain its own name.
This lets one model support different JSON contracts through different template ViewModels.
Related JSON and OCL features
AsTaJson creates JSON from a model object using a template ViewModel. ViewModelAsJSon returns the content of a ViewModel as JSON and supports using a ViewModel attribute's Presentation name as the JSON node name. Documentation:Tajson covers the wider TaJson approach, including receiving and applying JSON.
