MergeTaJson is an OCL operator that imports JSON data into model objects using a ViewModel as a field mapping guide. It reads key-value pairs from a JSON string and writes them into the attributes of a target object, using the ViewModel's column definitions to determine how JSON field names map to object attributes.
Syntax
object.MergeTaJson(viewmodelname,json)
How It Works
When MergeTaJson is called, MDriven executes a simple three step process:
1. Reads the JSON string you provide
2. Looks up the column definitions in the ViewModel you passed
3. For each column, writes the matching JSON value into the corresponding attribute on self
The ViewModel column definition is the key to understanding the mapping. The left side of each column is the JSON key name. The right side is the object attribute that receives the value:
| 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. |
The ViewModel as a Mapping Template
The ViewModel you pass into MergeTaJson is not rendered or displayed anywhere. It serves purely as a field mapping guide. You can create a dedicated lightweight ViewModel for this purpose, containing only the columns you want to map.
For example, if your ViewModel ArticlesJsonTemplate defines this column:
author_name : self.author
Then providing "author_name": "Buyondo" in your JSON will write "Buyondo" into self.author on the target object — even though the JSON key and the attribute name are different.
Examples
Example 1 — Partial field update from a hardcoded JSON string
This example comes from the ArticlesViewModel action editor. The action updates only the author field of the currently displayed article, leaving all other fields untouched.
vCurrent_Root.MergeTaJson(Articles1.ViewModels.ArticlesJsonTemplate,'{
"author_name": "Buyondo"
}'
)
Example 2 Copying one object into another via a modal selection
This example comes from the ArticlesSeekerViewModel action editor, placed in the Expression After Modal OK slot. The user selects an article from a popup seeker, and its data is merged into the currently open article.
vCurrent_Article.MergeTaJson( Articles1.ViewModels.ArticlesJsonTemplate, vModalResult_vCurrent_Articles1.AsTaJson(Articles1.ViewModels.ArticlesJsonTemplate, false) )
| 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. |
See also: Tajson
