You can use JSON with MDriven Designer and ViewModels to import external data, map it to model objects, transform it, and return custom REST responses.
Choose a JSON strategy
JSON data can enter or leave your application through a string. Choose the operator based on whether the JSON structure already matches your model or requires a ViewModel-defined mapping.
| Need | Use | Example |
|---|---|---|
| Import JSON whose property and association names match model attributes and associations | JsonToObjects | A JSON property "make" populates a model attribute named make.
|
| Create JSON, import JSON, or transform JSON using a ViewModel as the template | TaJson | A ViewModel column can map JSON key author_name to the author attribute.
|
| Update an existing object from selected JSON fields | MergeTaJson | Import {"author_name":"Buyondo"} into the current Article without changing other attributes.
|
Import JSON that matches your model
JsonToObjects is an OCL operator available in a ViewModel. It creates a new object hierarchy from a JSON string, starting with the type you provide. It captures only attributes and associations available in the model; JSON properties that have no matching model member are ignored.
Use JsonToObjects when your JSON names and model names match. It has no transformation layer between the JSON and the model, and it always creates new objects.
Create matching classes from sample JSON
You can use MDriven Designer to create a starting model structure from a copied JSON example.
- Copy the JSON text, including the outer curly braces
{}. - In MDriven Designer, right-click the class that will be the JSON root.
- Select Add attributes, associations, and classes from clipboard json or xml.
- Keep only the generated attributes and associations that you need to save.
For example, if the copied JSON contains {"make":"Volvo","models":[...]}, the generated root class can have a make attribute and a models association.
Call JsonToObjects
Store the received JSON in a string variable, then call JSonToObjects on selfVM.
selfVM.JSonToObjects(APIResult, vData)
In this example, APIResult is the root class type and vData is the JSON string. The operator creates an APIResult object and populates matching attributes and associations.
Use TaJson for template-based JSON
TaJson is a declarative JSON approach that uses a ViewModel as the template for both reading and writing JSON hierarchies. Use it when an external JSON contract differs from your model, when you need to control output shape, or when you need to update an existing object hierarchy.
For example, a ViewModel column can use author_name : self.author. TaJson then reads or writes the JSON key author_name while the model attribute remains author.
Create JSON with AsTaJson
Use AsTaJson to create a JSON object graph from an object and a template ViewModel.
SysSingleton.oclSingleton.AsTaJson(SysSingleton.ViewModels.ArticlesJSONTemplate,false)
The final false includes attributes even when they are empty. The template ViewModel determines the JSON hierarchy and field names.
Update an existing object with MergeTaJson
MergeTaJson reads JSON key-value pairs and writes matching ViewModel-defined fields into an existing target object.
vCurrent_Root.MergeTaJson(
Articles1.ViewModels.ArticlesJsonTemplate,
'{"author_name":"Buyondo"}'
)
Here, the template column author_name : self.author writes Buyondo to vCurrent_Root.author. Fields not included in the JSON remain unchanged.
Control TaJson output
Set these tagged values to true on a multi-nesting column in the template ViewModel when the required JSON shape differs from the default list of objects.
| Tagged value | Output | Example |
|---|---|---|
TaJsonTreatListAsDynamicProperties
|
Uses Name and Value properties from each nested row as JSON property names and values.
|
Rows with Name = "toolA" and a corresponding value can produce a property named "toolA". It also captures otherwise unmatched input JSON properties as Name/Value pairs when importing.
|
TaJsonTreatListAsValues
|
Creates an array from the first column in the nesting instead of an array of objects. | A nesting with values value1 and value2 becomes an array of values rather than objects containing column names.
|
RawJSon
|
Inserts already formatted JSON without serializing it as a quoted string. | A column containing {"enabled":true} is emitted as JSON content. The column value must be valid JSON.
|
Return custom JSON from a REST endpoint
When a ViewModel is exposed as a REST endpoint with Eco.RestAllowed: True, MDriven normally serializes the ViewModel content. To return JSON created by AsTaJson, add a ViewModel column named exactly RawJson and set its expression to the AsTaJson call. MDriven uses the returned string as the complete HTTP response body.
For example, a REST ViewModel can have a RawJson column with this expression:
SysSingleton.oclSingleton.AsTaJson(SysSingleton.ViewModels.ArticlesJSONTemplate,false)
For REST POST processing, use TaJson or JsonToObjects to apply the received JSON string to modeled objects. See Expose REST Service for REST endpoint behavior, response overrides, and request handling.
