MergeTaJson is an OCL operator for EAL actions that imports JSON into an existing model object by using a ViewModel as the mapping template.
Syntax
object.MergeTaJson(viewmodelname, json)
objectis the model object to update.viewmodelnameidentifies the ViewModel that defines the JSON-to-model mapping.jsonis the JSON string to import.
The operator returns a string containing the Merge/Apply log. Capture or inspect this result when you need to diagnose an import.
Map JSON fields with a ViewModel
The ViewModel passed to MergeTaJson is the mapping template. It is not rendered for this operation. You can therefore create a small dedicated ViewModel that contains only the fields that an import may update.
For each ViewModel column, the left side defines the JSON key and the right side identifies the attribute on the target object. For example, this column maps the JSON key author_name to the author attribute of the receiving object:
author_name : self.author
With that mapping, this JSON:
{
"author_name": "Buyondo"
}
updates self.author to Buyondo. The JSON key does not need to have the same name as the model attribute.
Here is how the ViewModel columns map to JSON keys and object attributes:
| ViewModel column | JSON key | Result on object |
|---|---|---|
| author_name : self.author | "author_name" | self.author |
| title : self.title | "title" | self.title |
| url : self.url | "url" | self.url |
| Key insight
The JSON key name does not need to match your attribute name. You define the mapping in the ViewModel. This lets you consume APIs that use different naming conventions from your model without changing either side. Partial merges are supported You do not need to include every attribute in the JSON string. Only fields present in the JSON will be updated. All other attributes on the object remain completely unchanged. |
Update selected fields
Use a ViewModel template that exposes only the fields you intend to import. A JSON payload containing a mapped field updates that field on the receiving object; this makes the operator suitable for a targeted update.
For example, the following action expression updates the author of the current root article using the ArticlesJsonTemplate mapping:
vCurrent_Root.MergeTaJson(Articles1.ViewModels.ArticlesJsonTemplate,'{
"author_name": "Buyondo"
}'
)
In this example, the template maps author_name to the article's author attribute. Fields that are not part of this JSON update are not the purpose of this operation.
Copy mapped values from another object
You can combine MergeTaJson with AsTaJson to transfer values between objects through the same ViewModel template. This ensures that the export and import use the same field names and mapping.
For example, place the following expression in the Expression After Modal OK slot of an action that opens an article seeker. After the user selects an article, the selected article is exported as JSON and merged into the currently open article:
vCurrent_Article.MergeTaJson(
Articles1.ViewModels.ArticlesJsonTemplate,
vModalResult_vCurrent_Articles1.AsTaJson(Articles1.ViewModels.ArticlesJsonTemplate, false)
)
Here:
vModalResult_vCurrent_Articles1is the article selected in the modal seeker.AsTaJson(..., false)creates JSON based onArticlesJsonTemplate.vCurrent_Articlereceives the mapped values.
The following example demonstrates each component of this pattern:
| Part | What it does |
| vCurrent_Article | The target — the article currently open that will receive the incoming data |
| Once by AsTaJson to convert the selected object to JSON, and again by MergeTaJson to map that JSON into the target | |
| The object the user selected inside the popup. The vModalResult_ prefix is required without it you read from the parent ViewModel, not the modal | |
| Converts the selected object to a JSON string on the fly, which is passed directly into MergeTaJson as the data source |
| Result
After the user clicks OK, all mapped fields from the selected article are written into the currently open article. The user can then choose to save or cancel — the merge does not auto-save. |
Import nested objects and associations
MergeTaJson can also import a JSON object hierarchy when the ViewModel contains nested ViewModel classes and associations. The root object must be supplied to the ViewModel class; the JSON then updates the hierarchy below that root according to the ViewModel structure.
The import recognizes the following ViewModel conventions:
| Convention | Purpose during import |
|---|---|
<ViewModelColumn>_AddNew action
|
Creates an object needed for an association named by the ViewModel column. The action must return the created object and must add that object to the association. If no such action is found, the import uses type information to create the object. |
vImportKey:string variable
|
Receives the import key before an object is created. An _AddNew action can use it to find an existing object instead of creating a new one.
|
Delete action in a nesting
|
Is used, when present, to delete an object that is missing from the input when merge mode is not in effect. |
CleanUpAction on the root ViewModel class
|
Runs after the import finishes. Prepare it as a column for an action in the ViewModel Editor. |
Handle large existing collections
By default, updating a many association can load all existing objects in that association so the import can locate objects by the IDs in the JSON. For a large dataset, this can be slow and consume too much server memory.
To control that lookup, add an action named <ViewModelColumn>_Lookup. The import calls this action and expects it to populate the many association. Before the call, vImportKey contains a comma-separated list of keys from the JSON. Use a collection variable as the expression of the many association, and let the lookup action populate that variable with the matching existing objects.
For the complete JSON hierarchy rules, association behavior, and JSON generation options, see Documentation:Tajson.
Check the import log
Both MergeTaJson and ApplyTaJson return a potentially large log string. Use the returned value when validating an import or investigating unexpected mapped values.
vImportLog := vCurrent_Root.MergeTaJson(
Articles1.ViewModels.ArticlesJsonTemplate,
'{
"author_name": "Buyondo"
}'
)
