🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
Import xml and JSon with MDriven
This page was created by Alexandra on 2017-10-28. Last edited by Wikiadmin on 2026-07-29.

You can import XML or JSON text into new objects in your model from a ViewModel by using selfVM.XmlToObjects or selfVM.JsonToObjects.

Use these OCL operations when the incoming data hierarchy already matches attributes and associations in your model. Both operations create a new object of the root class you specify, then populate matching parts of its object graph.

Choose the import operation

Incoming text Operation What happens
XML selfVM.XmlToObjects(RootClass, XmlText) MDriven converts the XML to JSON, then imports matching attributes and associations.
JSON selfVM.JsonToObjects(RootClass, JsonText) Imports matching attributes and associations directly from the JSON string.

JsonToObjects is available only in a ViewModel. It does not provide a transformation layer: JSON property names and the relevant model attribute or association names must match. It always creates new objects; it does not update an existing object hierarchy. For an approach based on a ViewModel template, see JsonToObject vs Tajson.

Example input

This example imports two people from XML:

<?xml version='1.0' standalone='no'?>
<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>

When MDriven converts this XML to JSON, its relevant structure is:

{
  "root": {
    "person": [
      {
        "@id": "1",
        "name": "Alan",
        "url": "http://www.google.com"
      },
      {
        "@id": "2",
        "name": "Louis",
        "url": "http://www.yahoo.com"
      }
    ]
  }
}

The XML declaration is also represented in the converted JSON as ?xml. You do not need to model it unless you need to retain it.

Model the incoming hierarchy

Create a root class for the import and classes that represent the nested objects. For the example above, the model can have this shape:

Model element Name Purpose
Class ImportFile The root class passed to XmlToObjects or JsonToObjects.
Association root A single-valued association from ImportFile to the class representing the root object.
Association person A multi-valued association from the root class to Person, because the input contains an array of people.
Attributes on Person name, url Values populated from each person object.

Association names describe nested objects. Attribute names describe scalar values. In the example, root is one nested object, while person is a collection because the JSON contains an array.

The input property @id is not the same name as id. If you need to import that value, make sure the target model element matches the incoming property name. Do not assume that XML attribute names are automatically renamed.

Generate a starting model from sample data

Instead of creating the structure by hand, you can generate a class template from a JSON or XML sample:

  1. Copy the complete JSON or XML sample to the clipboard. For JSON, include the outer curly braces.
  2. In MDriven Designer, right-click the class that will be the import root.
  3. Select Add attributes, assoc. and classes from clipboard json or xml.
  4. Review the generated classes, attributes, and associations. Keep the elements you need and remove the rest.

For the XML shown above, the generated structure can include classes such as Root1 and Person1. Review the resulting names before writing the import expression. See Documentation:Using JSON or XML as class template for the complete procedure.

Add input to a ViewModel

Expose a String attribute in a ViewModel to hold the incoming text. For example, expose XmlInput on an object that the user can edit. In the Prototyper, create that object and paste the XML into XmlInput.

Add an action that imports the text. The action must execute in the ViewModel because selfVM.XmlToObjects and selfVM.JsonToObjects are ViewModel operations.

Import XML

Use an action expression such as:

vNewImportFile := selfVM.XmlToObjects(ImportFile, self.XmlInput)

This expression:

  1. Takes the XML string from self.XmlInput.
  2. Creates a new ImportFile object as the import root.
  3. Creates and connects matching nested objects, such as the root object and its people.
  4. Assigns the resulting ImportFile object to vNewImportFile for use later in the action.

For example, after importing the sample XML, the imported hierarchy contains a root with two related Person objects. Their name values are Alan and Louis, and their url values contain the corresponding URLs.

Import JSON

If the incoming text is already JSON, expose it in an attribute such as JsonInput and use:

vNewImportFile := selfVM.JsonToObjects(ImportFile, self.JsonInput)

The root class and the hierarchy-matching rules are the same as for XML. For operator-specific details, see JsonToObjects.

Inspect the converted JSON while developing

You can add an optional String attribute named RawJSon to the root target class, for example ImportFile. When that attribute exists on the class used as the target of XmlToObjects or JsonToObjects, MDriven assigns the handled JSON to it.

For XML imports, this lets you inspect the JSON produced from the XML before object matching occurs. It is useful when you need to determine whether an input element becomes an object, an array, or an attribute in the JSON structure.

Matching rules and limitations

  • Matching is based on attribute and association names, not on class names. Your classes can have any names.
  • An input object maps through an association whose name matches the JSON property.
  • An input array maps through a multi-valued association whose name matches the JSON property.
  • An input scalar value maps to an attribute whose name matches the JSON property.
  • Input properties that MDriven cannot find in the model are ignored without an error.
  • Model attributes and associations that have no corresponding input data are allowed; they remain unpopulated by that input.
  • The operations create new objects. If you need to update an existing hierarchy or shape data through a ViewModel template, evaluate TaJSon.

Verify the import

  1. Run the ViewModel in the Prototyper.
  2. Create or select the object that exposes XmlInput or JsonInput.
  3. Paste the sample input.
  4. Execute the import action.
  5. Show the associations from vNewImportFile, or otherwise expose the imported root and its nested objects in the ViewModel.
  6. Confirm that the expected people and attribute values were created.
  7. If values are missing, inspect RawJSon and compare every input property name with the model's attributes and associations.

See also