You can generate a model structure from a sample JSON or XML payload when you need classes, attributes, and associations that JsonToObjects can import into.
When to use this feature
Use Add attributes, assoc. and classes from clipboard json or xml in MDriven Designer when you have a representative message and want to create a starting model section that follows its hierarchy.
For example, an external service may provide people in this XML shape:
<root>
<person id='1'>
<name>Alan</name>
<url>http://www.google.com</url>
</person>
<person id='2'>
<name>Louis</name>
<url>http://www.yahoo.com</url>
</person>
</root>
You can use this sample to create the classes and relationships needed to receive the data. The generated structure is a starting point: keep only the data your application must store and adjust the model before implementing the import.
Create a model template from the clipboard
- In MDriven Designer, create or select the class that will be the root of the incoming data. For this example, create a class such as
ImportFile. - Copy a complete representative JSON or XML message to the clipboard.
- For JSON, include the outer curly braces (
{and}) in what you copy. The outer object defines the root data shape. - Right-click the root class.
- Select Add attributes, assoc. and classes from clipboard json or xml.
- Review the generated attributes, associations, and classes in the model.
For the XML example, MDriven Designer creates a structure corresponding to the nested root and repeated person elements. The generated class names may be numbered, such as Root1 and Person1.
Read the generated structure
The import works by matching names in the incoming data with attribute and association names in your model. The class names themselves do not have to match the message.
| Incoming data shape | Model structure to retain | Example |
|---|---|---|
| A nested object or element | A single-valued association | root is represented by a link from the import root to a root object.
|
| A repeated object or element | A multi-valued association | The two person elements become objects in a person association.
|
| A scalar value | An attribute | name and url become attributes of each person object.
|
In this example, the intended result is an ImportFile object with a link to the imported root, whose multi-valued person association contains two person objects. Each person has values for name and url.
Clean up before importing
Remove attributes and associations that you do not need to save. You may also add attributes and associations that are not present in the sample; they remain empty when the message does not contain matching data.
Names are significant for the parts that receive data:
- An incoming property or element is applied only when a matching attribute or association exists in the model.
- Input that has no matching attribute or association is ignored without an error.
- Extra model attributes or associations are allowed; they do not prevent the import.
- Repeated data must be represented by a multi-valued association if you want to retain all occurrences.
This behavior makes the generated model useful as a data-shape draft, but you must verify it against the messages your integration actually receives.
Import into the generated model
After creating the model structure, expose the incoming JSON or XML string in a ViewModel. Then call the appropriate operation from an action in that ViewModel.
For XML, the operation is selfVM.XmlToObjects. The following expression creates an ImportFile root object from the XML stored in self.XmlInput and assigns it to a local variable:
vNewImportFile:=selfVM.XmlToObjects(ImportFile, self.XmlInput)For JSON, use selfVM.JSonToObjects with the target root class and the JSON string:
vNewImportFile:=selfVM.JSonToObjects(ImportFile, self.JsonInput)JSonToObjects creates new objects starting at the type supplied as its first argument. It follows matching association and attribute names to create the object tree. It is available in a ViewModel and does not provide a transformation layer between the JSON and your model. For the complete XML and JSON import walkthrough, see Documentation:Import xml and JSon with MDriven.
Inspect converted XML during development
XML is converted to JSON before MDriven handles it as objects. To inspect the JSON used during import, add an optional string attribute named exactly RawJSon to the target root class. When that class is used as the target of selfVM.XmlToObjects or selfVM.JSonToObjects, MDriven assigns the handled JSON to RawJSon.
This is useful when checking how XML elements and repeated elements are represented before you finalize association multiplicities and names.
Choose JsonToObjects or Tajson
Use this clipboard feature when you want a model section whose names directly match an incoming message for JSonToObjects or XmlToObjects.
| Need | Use | Why |
|---|---|---|
| Create new objects from JSON or XML whose names match your model | selfVM.JSonToObjects or selfVM.XmlToObjects
|
These operations create a new object tree from the supplied root type. |
| Map, transform, receive, or send JSON through a ViewModel-defined shape | Tajson | Tajson uses a ViewModel as the template for the JSON hierarchy rather than requiring direct model-name matching. |
Read Documentation:JsonToObject vs Tajson before choosing between the two approaches. If you need a ViewModel-defined mapping for updating an existing object, see Documentation:OCLOperators MergeTaJson.
Use this with REST input
A REST endpoint can receive JSON and apply it to modeled objects. Build the target model section from a representative payload first, then choose either Tajson or JSonToObjects for the endpoint logic. See HowTos:Expose REST Service for REST-specific setup and request handling.
Limits
This feature creates a model draft from a clipboard sample. It does not import an XSD or WSDL definition. For importing a web-service interface definition as a model, see Documentation:Importing web service interface as model.
