nestedKey Element¶
The <nestedKey>
element defines nested key fields and their generation methods within a data generation task. It allows you to structure complex data in a hierarchical format, such as dictionaries (dict
) or lists (list
), and control its content dynamically.
Attributes¶
- name: Specifies the name of the nested key. This is mandatory.
- type: Defines the type of the nested key. It can be
list
ordict
. - count: Specifies the number of items to generate for nested keys, applicable for
list
types. - source: Specifies the data source for the nested key.
- sourceScripted: Determines whether the source is scripted (optional).
- cyclic: Enables or disables cyclic behavior for data generation (optional).
- separator: Defines a separator used between multiple key values (optional).
- condition: Specifies a condition for the inclusion of the nested key.
- defaultValue: Sets a default value when the condition is false.
- script: Specifies a script for generating dynamic content for the nested key.
- minCount and maxCount: Define the minimum and maximum counts for generating keys.
- distribution: Controls how data from the source is distributed (
random
,ordered
). Default israndom
. - converter: Specifies a converter to transform nestedKey's children values.
- variablePrefix: Configurable attribute that defines the prefix for variable substitution in dynamic strings (default is __).
- variableSuffix: Configurable attribute that defines the suffix for variable substitution in dynamic strings (default is __).
Children¶
- key: Defines key-value pairs within the nested structure.
- variable: Variables can be used within the nested structure.
- nestedKey: Allows additional nesting within a nested key.
- execute: Executes operations within the nested structure.
- if: Defines conditional logic inside the nested key.
- list: Embeds lists within the nested key.
- echo: Outputs data for logging/debugging.
- element: Specifies child elements for XML generation.
- array: Embeds arrays within the nested key.
Example 1: Nested Dictionary¶
Warning
The examples provided are intended to demonstrate the syntax and structure of DATAMIMIC models and features. They may not work directly out of the box. For fully functional examples, please refer to the demo section available in our demo store when you log in. If you are missing specific use cases or scenarios, feel free to reach out to us—we're happy to assist.
The following example creates a nested dictionary (dict
) with keys like street
, city
, and zip
.
1 2 3 4 5 |
|
Output:
1 2 3 4 5 6 7 |
|
Example 2: Nested List¶
This example generates a nested list (list
) with two items, each containing brand
and manufacturing_date
keys.
1 2 3 4 5 6 7 8 9 10 |
|
Output:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Example 3: Using condition
and defaultValue
¶
This example shows how to apply conditions and default values when generating nested keys.
1 2 3 4 5 6 7 8 |
|
Output:
1 2 3 4 5 6 7 8 9 |
|
Example 4: Enriching or Overwriting Nested Keys from a Template¶
You can enrich or overwrite nested keys from an existing template. Here’s how to add a field (country
) to an existing address
key without specifying the type:
1 2 3 4 5 |
|
Output:
1 2 3 4 5 6 7 8 |
|
To overwrite a nested key entirely from a template:
1 2 3 4 5 6 7 |
|
Output:
1 2 3 4 5 6 7 |
|
Example 5: Using source put data into nestedKey¶
This follow example get data from source and put it to nestedKey, have to specify the right type
of input data.
1 2 3 4 5 6 |
|
data/person.ent.csv | |
---|---|
1 2 3 4 |
|
data/pet.json | |
---|---|
1 2 3 4 5 |
|
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Example 6: Using source with sourceScripted for dynamic value in source¶
This follow example get data from source, and specific values in source will be replaced by generated value. We call they dynamic variables.
When using dynamic variables to replace values in a source dataset, there are two ways to represent variables: {var}
and __var__
('__'
can be changed by setting 'variablePrefix' and 'variableSuffix').
Both allow dynamic replacement, but they differ in functionality, scope, and the type of value they return.
Using {var}:
- The
{}
must enclose the entire value, inside{}
only the variable name is allowed, no additional characters inside or outside. - The replaced value retains the data type of the generated variable. E.g. if the variable is an integer, the replaced value will also be an integer.
1 |
|
Using variablePrefix and variableSuffix:
- The variable must be enclosed by the prefix and suffix (default is
__
). - It supports combining multiple variables and text in the string containing
__var__
. - The replaced value is always a string, regardless of the original data type of the variable.
1 |
|
Example:
datamimic.xml | |
---|---|
1 2 3 4 5 6 7 |
|
Source data:
script/data.json | |
---|---|
1 2 3 4 5 6 7 8 9 |
|
Output (both age
and address_number
original are number, but with __var__
output is string and {var}
output is number)
1 2 3 4 5 6 7 8 9 |
|
Important:
- In source data, you can not have 'key' or 'header column' with the same name as dynamic variable name (e.g. you cannot use
"random_age": "__random_age__"
)
Example 7: Using script to get value into nestedKey
¶
This follow example get data from script
attribute, nestedKey
'type' will be set base on input value. Noted that input value type must be 'dict' or a 'list' of 'dict'.
1 2 3 4 5 6 7 8 |
|
Output
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Best Practices for Using <nestedKey>
¶
- Use Conditions and Default Values: Leverage conditions to dynamically control whether a nested key is generated, and use default values when the condition evaluates to
False
. - Nesting for Complex Data: Utilize nested keys to generate complex hierarchical structures like dictionaries and lists.
- Enrich or Overwrite Templates: Use
<nestedKey>
to enhance or completely overwrite nested data from predefined templates. - Efficient Distribution: Use the
distribution
attribute to control how data from the source is applied (random
,ordered
).