🚀 Welcome to MDriven Learn –  MDriven is now on Discord!  Don’t miss the latest Release Notes.
OCLOperators JSonToObjects
This page was created by Hans.karlsen on 2021-06-01. Last edited by Wikiadmin on 2026-07-29.

You can use selfVM.JsonToObjects in a ViewModel to create a new object hierarchy from a JSON string when your model attributes and associations match the JSON property names.

What JsonToObjects does

JsonToObjects reads a JSON string and creates a new object of the type you specify as the root. It then fills matching attributes and follows matching associations to create the related object hierarchy.

Use it when you receive JSON and want to store its values as objects in your model for further processing. The operator is available only in a ViewModel.

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

In this example:

Part Meaning
selfVM The current ViewModel.
ImportFile The model class to create as the root object.
self.JsonInput The string attribute containing the JSON text.
vNewImportFile A variable that receives the newly created ImportFile object.

Match the JSON structure to the model

JsonToObjects has no transformation layer. JSON property names must match model attribute names and association names.

  • A JSON value becomes a model attribute when the property name matches an attribute name.
  • A JSON object becomes a related object when the property name matches a single association.
  • A JSON array becomes multiple related objects when the property name matches a multi-valued association.
  • The class names do not need to match JSON property names. JsonToObjects uses the names of attributes and associations to determine where values belong.

For example, this JSON has a root object containing a person array:

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

Create the following model structure:

Model element Required name Purpose
Root class ImportFile The type passed as the first argument. Its name is your choice.
Single association from ImportFile root Receives the JSON root object.
Multi-valued association from the class at root person Receives every object in the JSON person array.
Attributes on the class at person id, name, url Receive the values from each person object.

The ImportFile, root, and person classes can have any class names. The associations must be named root and person, and the person attributes must be named id, name, and url.

Import JSON into model objects

  1. Create a class that will act as the import root, for example ImportFile.
  2. Add a String attribute to hold the incoming JSON, for example JsonInput.
  3. Expose JsonInput in a ViewModel so that a user or integration can provide the JSON text.
  4. Add the classes, attributes, and associations that correspond to the JSON structure.
  5. Add a ViewModel action that calls selfVM.JsonToObjects. Store the returned root object in a variable if later expressions need to use it.
  6. Run the action. JsonToObjects creates the root object and its matching related objects.

For the example structure, the action expression is:

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

Create a matching model from clipboard JSON

MDriven Designer can generate a starting class structure from JSON copied to the clipboard. This helps you establish the attributes and associations that JsonToObjects needs.

  1. Create the class that will act as the JSON root.
  2. Copy the JSON text, including the outer curly braces ({ and }).
  3. In MDriven Designer, right-click the root class.
  4. Select Add attributes, associations, and classes from clipboard json or xml.
  5. Review the generated classes, attributes, and associations.
  6. Keep only the attributes and associations that you need to save from the input.
  7. Verify that retained attribute and association names match the JSON properties that you intend to import.

The generated structure is a starting point. Remove unneeded elements before you make the import action part of your application.

What happens when names do not match

JsonToObjects silently ignores input that it cannot match to an attribute or association in the model. It also accepts model attributes and associations that are absent from the JSON.

For example, if the incoming JSON contains phone but the person class has no phone attribute or association, no phone value is imported. If the person class has a status attribute but the JSON has no status property, the import still succeeds.

This behavior makes name review important: a misspelled association or attribute can cause data to be skipped without an error.

Keep the handled JSON during development

You can add an optional String attribute named RawJSon to the class used as the target root type. When present, the handled JSON is assigned to that attribute. This can help you inspect the input during development.

Choose the appropriate JSON approach

JsonToObjects always creates new objects and maps directly by matching model names. If you need the ViewModel itself to define the JSON hierarchy for both reading and writing, use TaJson instead.

For importing XML through the related XML operator, including how XML is converted to JSON before object creation, see Documentation:Import xml and JSon with MDriven. For reading one property from JSON text rather than creating model objects, see JsonGetProp.

See also