> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arc.cdata.com/llms.txt
> Use this file to discover all available pages before exploring further.

# jsonDOMGet

> jsonpath マッピングを使用してJSON ドキュメントから値を取得します。繰り返し要素や配列もサポートします。

export const siteNameShort = "Arc";

JSON ドキュメントから値を取得します。

## 必要なパラメータ

* **map:\***：1つ以上のインプットのセット。マッピングされるパラメータの名前がコロンの前に置かれ（例：`map:foo`）、値はドキュメント内の対象のjson 要素へのjsonpath です（詳細は[例](#例)を参照してください）。

## オプションのパラメータ

* **uri**：JSON ファイルのURI。インプットファイルに対する`[FilePath]` などの動的なファイル参照、ディスク上の静的なファイルパス、またはJSON ファイルのパブリックURL を指定できます。
* **text**：JSON データへの読み取り可能なハンドル参照。このハンドルは[jsonOpen](./op-json-open) オペレーションによって作成され、インプットJSON がファイルやrawテキストではない場合、またはドキュメントのファイルデータにアクセスする複数のアクションを実行する必要がある場合に役立ちます。
* **handle**：JSON データへの読み取り可能なハンドル参照。このハンドルはjsonOpen オペレーションによって作成され、対象のJSON がインプットファイルではない場合にのみ必要です。詳細と例については[jsonOpen](./op-json-open) を参照してください。
* **repeatelement#**：ドキュメント内で繰り返される要素へのjsonpath。
* **repeatmode**：`repeatelement#` の値をアウトプットアイテムに格納する方法を定義します。使用できる値は`ITEM` および`ARRAY` です。`ITEM`（デフォルト）に設定すると、繰り返し要素から見つかった値は、オペレーションが生成する各アウトプットアイテムに個別の属性として格納されます。`ARRAY` に設定すると、繰り返し要素から見つかった値は単一のアウトプットアイテムに配列属性として格納され、1始まりのインデックスを介してアクセスする必要があります（例：`[result.color#2]` はresult アイテムの`color` 属性内の2番目の値を参照します）。

## アウトプット属性

* **\***：map パラメータを使用してマッピングされたJSON 要素からのアウトプット値。これらは、アウトプットアイテムとマッピングされたインプットアイテムの名前を参照してアクセスします（例：`[results.foo]`）。アウトプットアイテムの名前（この場合は`results`）はユーザーが決定します。追加のコンテキストについては[例](#例)を参照してください。

## 例

### JSON ドキュメントから値を取得する

次のJSON データについて考えてみます。これは{siteNameShort} の[Script コネクタ](../../connectors/script)へのインプットデータとして渡されます：

```json theme={null}
{
    "name": "Nancy",
    "age": "31",
    "gender": "Female"
}
```

Script コネクタでは、{siteNameShort}Script でjsonDOMGet を呼び出して、このデータで定義された人物の`name` および`age` を取得し、それらを{siteNameShort} のメッセージのヘッダーとして追加できます。次の{siteNameShort}Script コードは、その方法の例を示しています：

```xml theme={null}
<!-- Setting the input uri and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.map:name" value="/json/name" />
<arc:set attr="json.map:age" value="/json/age" />

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- result.name and result.age has the value of the json path because map:name and map:age was 
       set in the input to the op, and result is the name of the output item. Once this
       op closes, result falls out of scope, so use your value inside the arc:call block --> 
  <!-- Setting the values for name and age as headers on the message -->
  <arc:set attr="output.header:personsname" value="[result.name]" />
  <arc:set attr="output.header:personsage" value="[result.age]" />
</arc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
```

このコードが実行されると、{siteNameShort} は新しく追加されたヘッダーを持つファイルをアウトプットとしてフローの下流に送ります。

### JSON オブジェクト内の配列の特定のインデックスにアクセスする

JSON データ内の配列の特定のインデックスにアクセスする必要がよくあります。例えば、Script コネクタへのインプットファイルとして渡される次のJSON オブジェクト内で、`orders` 配列の2番目のorder オブジェクトの`id` にアクセスしたい場合があります。

```json theme={null}
{
    "warehouseid":"WH1234",
    "orders": [
        {
            "date": "02/27/2023",
            "id": "1234"
        },
        {
            "date": "02/28/2023",
            "id": "5678"
        },
        {
            "date": "03/01/2023",
            "id": "9876"
        }
    ]
}
```

Script コネクタでは、{siteNameShort}Script でjsonDOMGet を呼び出し、`orders` 配列内のいずれかのオブジェクトに存在する要素への1始まりのインデックスjsonpath を使用して、特定のorder の`id` にアクセスできます。次の{siteNameShort}Script コードは、その方法の例を示しています：

```xml theme={null}
<!-- Setting the input uri and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.map:id" value="/json/orders/\[2\]/id" />

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- Setting the result from the mapped id parameter as a header on the message -->
  <arc:set attr="output.header:orderid" value="[result.id]" />
</arc:call>

<!-- Setting the output file and pushing the file out -->
<arc:set attr="output.filepath" value="[FilePath]" />
<arc:push item="output" />
```

### repeatelement パラメータを使用する

次のJSON データについて考えてみます。これはScript コネクタへのインプットファイルとして渡されます：

```json theme={null}
{
    "hello": "world",
    "colors": [
        {
            "color": "yellow",
            "example": "banana"
        },
        {
            "color": "red",
            "example": "apple"
        },
        {
            "color": "orange",
            "example": "orange"
        }
    ]
}
```

Script コネクタ内で、次のコードがインプットファイルに対して実行されます。`repeatelement#` パラメータは、繰り返しの値を持つ要素を定義するために使用されます。この例では、`color` および`example` の値は、JSON 配列である`/json/colors` 要素内で繰り返されています。`repeatelement#` を設定すると、jsonDOMGet オペレーションはその要素に存在する値をループします。

`repeatmode` パラメータは、`repeatelement#` の値をアウトプットとして格納する方法を定義します。`ITEM`（デフォルト）に設定すると、繰り返し要素によって見つかった値は、アウトプットアイテムに個別の属性として格納されます。`ARRAY` に設定すると、繰り返し要素によって見つかった値は、アウトプットアイテムに配列属性として格納され、1始まりのインデックスを介してアクセスする必要があります（例：`[result.color#2]` は`red` に解決され、`result.example#2` は`apple` に解決されます）。

```xml theme={null}
<!-- Initializing the output data -->
<arc:set attr="out.data" />
<!-- Setting the input uri, repeatmode and map parameters -->
<arc:set attr="json.uri" value="[FilePath]" />
<arc:set attr="json.repeatelement#" value="/json/colors/" />
<arc:set attr="json.repeatmode" value="ITEM" />
<!-- Note, the values for the map are relative to the repeatelement path -->
<arc:set attr="json.map:color" value="color" />
<arc:set attr="json.map:example" value="example" /> 

<!-- Calling the operation, passing in the json item and creating a "result" output item -->
<arc:call op="jsonDOMGet" in="json" out="result">
  <!-- Setting the output data to the enumerated results of the call -->
  <arc:set attr="out.data">[out.data]
    <arc:enum list="color,example" separator=",">[_value]: [result.[_value]]\n
    </arc:enum>
  </arc:set>
</arc:call>

<!-- Setting the output filename and pushing the file out -->
<arc:set attr="out.filename" value="data.txt" />
<arc:push item="out" />
```

アウトプットファイル：

```
color: yellow
example: banana
color: red
example: apple
color: orange
example: orange
```
