You can use ApplyTaJson in an EAL action to apply JSON to an object hierarchy defined by a TaJson ViewModel, including removing missing objects from multi-associations.
Syntax
object.ApplyTaJson(viewmodelname,json)
| Part | Meaning |
|---|---|
object
|
The root object to update. The import starts at this object. |
viewmodelname
|
The ViewModel that defines the JSON structure, attributes, associations, and import behavior. |
json
|
The JSON text to apply. |
The operator returns a string containing the ApplyTaJson log. The log can be large, so plan how you will inspect or store it when diagnosing an import.
What ApplyTaJson does
ApplyTaJson reads JSON properties and associations that match the ViewModel structure below the supplied root object. It updates the corresponding object hierarchy.
For a multi-association, ApplyTaJson is an apply operation: objects currently in the association that are not represented in the JSON are removed. This is the key difference to a merge-style import. Use ApplyTaJson only when the payload represents the intended complete contents of each processed multi-association.
For example, if an order's ViewModel exposes an Lines multi-association and the current order has lines A, B, and C:
- JSON containing A and B causes C to be removed from that association.
- JSON containing A, B, and C retains all three.
If the payload represents only additions or partial updates, review TaJson and use the appropriate merge behavior instead of ApplyTaJson.
Apply JSON in an EAL action
- Create or select the object that will be the import root.
- Create a ViewModel whose root is configured for that object and whose nested ViewModel classes match the JSON hierarchy you intend to import.
- Obtain the JSON text.
- Call ApplyTaJson on the root object.
- Capture the returned log and inspect it if the import does not produce the expected result.
For example, an action can follow this shape:
let importLog = rootObject.ApplyTaJson(importViewModelName, jsonText) in
importLog
In this example, rootObject is the object to update, importViewModelName identifies the ViewModel that describes the payload, and jsonText contains the JSON document. Use the returned importLog to investigate the apply operation.
Configure the ViewModel for import
The ViewModel is the contract between the JSON document and the object model. JSON properties iterate attributes and associations with matching names in the ViewModel hierarchy. The ViewModel root must receive the root object used for the import.
TaJson recognizes the following ViewModel elements during object creation and removal:
| ViewModel element | ApplyTaJson behavior |
|---|---|
Action <ViewModelColumn>_AddNew
|
Used to create an object needed for the association named by <ViewModelColumn>. The action must return the created object and must add that object to the association. If no action exists, TaJson uses type information to create the object.
|
Variable vImportKey:string
|
Updated before an object is created. An _AddNew action can use it to find an existing object.
|
Action Delete in a nesting
|
Used, when present, when ApplyTaJson needs to delete an object because it is missing from the input. This deletion behavior does not apply in merge mode. |
Root action CleanUpAction
|
Called after the import finishes when it is prepared as an action column on the root ViewModel class. |
Check ReadOnly before importing
When you add attributes to multi-nestings, the framework sets ReadOnly to true. This is often appropriate for grid cells in a user interface, but in TaJson it means that the value is read-only during import as well.
Before using ApplyTaJson, inspect the ReadOnly setting for every attribute that the JSON must update. For example, if a nested Quantity attribute is marked read-only, supplying "Quantity" in the JSON will not make it an editable import field.
Handle large existing associations
By default, TaJson loads objects in a many association so that it can match them with IDs in the JSON. For very large associations, this can be slow and can exhaust server memory.
To control this lookup, add an action named <ViewModelColumn>_Lookup for the many-association column:
- Add a variable that holds the collection of objects to update.
- Use that variable as the expression for the many association.
- Add the matching
_Lookupaction. - In the action, use
vImportKey, which contains a comma-separated list of keys from the JSON, to load and add the required objects to the collection.
For example, for a ViewModel column named Invoices, create an Invoices_Lookup action. When TaJson calls it, populate the association collection with the invoice objects matching the keys supplied in vImportKey. TaJson then continues with the normal import.
Important: ApplyTaJson removes existing objects that are not found in the JSON. When you use a lookup action with ApplyTaJson, the collection you provide must include the objects that ApplyTaJson needs to consider for removal. A lookup that returns only the objects named in the payload can make the deletion decision incomplete.
Split very large or deep JSON
A deeply nested payload can require TaJson to create and resolve too many objects in one operation. For example, 1,000 detail objects, each with 1,000 child objects, each with 1,000 further children, represents 1,000 × 1,000 × 1,000 objects to manage at once.
Use a ViewModel column named exactly RawJSon (case-sensitive) to split such work:
- Create a first-pass ViewModel that imports a manageable top-level section.
- Add a
RawJSon:stringcolumn at the nesting where later processing should begin. - Run ApplyTaJson. TaJson places the JSON for the current level and below into
RawJSonas text. - Process stored RawJSon values in smaller batches with a second ViewModel and a separate ApplyTaJson call.
This approach lets a worker process small pieces over time rather than attempting the entire deep hierarchy in one import. See TaJson for the RawJSon example and broader JSON import and export behavior.
