🧩
New! Render PlantUML diagrams directly inside GitHub with our official browser extension — No server. No tokens. No tracking. Zero permissions but clipboard. — Try it out and let us know what you think!

JSON前処理

PlantUMLには、いくつかのJSON前処理機能が搭載されており、すべてのダイアグラムで利用できます。

これは、既存の前処理機能を拡張します。

🛈 JSONデータを表示する方法を探している場合は、「JSONデータを表示する」を確認してください。

変数定義

既存の型(StringInteger)に加えて、JSON型の変数を定義できます。

例:

!$foo = { "name": "John", "age" : 30 }

これは、次のような構造に対応します:

WARNING
 This translation need to be updated. 
WARNING

データへのアクセス

変数を定義すると、そのメンバにアクセスすることができます:

WARNING
 This translation need to be updated. 
WARNING

複雑な構造

複数行にわたる、複雑なJSONオブジェクトや配列を定義することもできます。

$fooを定義します:

次のように、値にアクセスできます:

もしくは、中間変数を使用して、 値にアクセスできます:

データのロード

標準関数を使用して、URLまたはローカルファイルからJSONオブジェクトをロードできます:

!$foo = %load_json("http://foo.net/users/list.json")
!$foo2 = %load_json("myDir/localFile.json")

1.2021.15 以降で利用可能

繰り返し [foreach]

配列を定義すると、それに対してループ処理を行うことができます。

[SW] Some remarks

[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
WARNING
 This translation need to be updated. 
WARNING

Full Example

From a example worked in a forum question, with this JSON structure:

Self-descriptive example

Here is a self-descriptive example:

[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.

[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).

[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).

[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

[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).

[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).

[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).

[Ref. PR-1761]

[Ref. PR-1782]

%json_merge builtin function

You can use %json_merge to merge JSON structures (returns the final JSON structure).

[Ref. PR-1763]

%str2json builtin function

You can use %str2json to transform a string value to a JSON structure.

Here are some example:

🚩 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]