Preprocessing JSON
Some JSON preprocessing capabilities are included in PlantUML, and available for all diagrams. That extends the current preprocessing. 🛈 If you are looking for how to display JSON data: see rather Display JSON Data.
Variable definition
In addition to existing type (String and Integer), you can define JSON variable.
JSON Object
Example:
!$foo = { "name": "John", "age" : 30 }
Corresponding of this structure:
|
🎉 Copied!
|
|
JSON Array
Example:
!$foo = ["abc", "def", "ghi"]
Corresponding of this structure:
|
🎉 Copied!
|
|
Access to data
Once a variable is defined, you can access to its members:
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
Complex structures
It is possible to use complex JSON objects and arrays, with definition on several lines.
Let $foo, the structure:
|
🎉 Copied!
|
|
You can acess to the values:
$foo.employees[0].name$foo.employees[0].salary
|
🎉 Copied!
|
|
Or using intermediate variables:
!$attribute_1="name"!$attribute_2="salary"
$foo.employees[0][$attribute_1]$foo.employees[0][$attribute_2]
|
🎉 Copied!
|
|
Loading data
Some standard function provides a way to load JSON object from URL or local files:
!$foo = %load_json("http://foo.net/users/list.json")
!$foo2 = %load_json("myDir/localFile.json")
Available since 1.2021.15
Loop [foreach]
If you define array, you can loop over.
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
[SW] Some remarks
- for or better foreach ? ->
foreach- It would be nice to also have "break" and "continue"
- It would be nice to also have the for or while loop with a standard variable
Full Example
From a example worked in a forum question, with this JSON structure:
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
Self-descriptive example
Here is a self-descriptive example:
|
🎉 Copied!
|
|
[Adapted from QA-12917]

%get_json_keys builtin function
You can use %get_json_keys to get all the keys of one level on a JSON structure.
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
[Ref. QA-15360, QA-15423]

%get_json_type builtin function
You can use %get_json_type to get the type of an element of a JSON structure (returns a string).
|
🎉 Copied!
|
|
[Ref. QA-15360]

%json_key_exists builtin function
You can use %json_key_exists to know if a key exists on a JSON structure (returns a boolean).
|
🎉 Copied!
|
|
[Ref. QA-15423]

%size builtin function
You can use %size to know the size of different elements on a JSON structure.
For each type here are the return value:
| Type | Return value |
JSON Object
|
the number of pairs it contains |
JSON Array
|
the number of values it contains |
string value
|
the number of characters it contains |
numeric value
|
zero |
true/false/null
|
zero |
|
🎉 Copied!
|
|
[Ref. QA-14901]

%json_add builtin function
You can use %json_add to add element on an existing JSON structure (returns the final JSON structure).
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
[Ref. PR-1742 and PR-1757]

%json_remove builtin function
You can use %json_remove to remove element on an existing JSON structure (returns the final JSON structure).
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
[Ref. PR-1742]

%json_set builtin function
You can use %json_set to set element on an existing JSON structure (returns the final JSON structure).
|
🎉 Copied!
|
|
[Ref. PR-1761]
|
🎉 Copied!
|
|
[Ref. PR-1782]

%json_merge builtin function
You can use %json_merge to merge JSON structures (returns the final JSON structure).
|
🎉 Copied!
|
|
[Ref. PR-1763]

%str2json builtin function
You can use %str2json to transform a string value to a JSON structure.
Here are some example:
|
🎉 Copied!
|
|
|
🎉 Copied!
|
|
🚩 Known issue: Unfortunately, be careful with quote management. Some issues occur when a single quote is contained in a string when the entire string is surrounded by single quote.[Ref. PR-1742]