---
schema_version: "1"
id: 0241169b35d64dbd624f8d6ea0e51ddd889379c6f5e79589279dd90a6152e7b1
title: DataWeave を使用した型の強制
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.8/dataweave-types-coercion.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-types-coercion
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 0f5371528ecd632a9d5af83d76e9c29fdbd3cf30c93d3be0d95dc2a19d2de6a6
description: MuleSoft Documentation Site
release: "2.8"
latest: false
---
# DataWeave を使用した型の強制

DataWeave では、​`as`​ 演算子を使用して、型を別の型に強制的に変換できます。型強制は実行時に適用されます。 開始する前に、Mule 4 アプリケーションでは DataWeave のバージョン 2.x が使用されることに注意してください。Mule 3 アプリケーションでの DataWeave については、​[DataWeave バージョン 1.2 のドキュメント](#1.2@dataweave::index.adoc)​を参照してください。 他の Mule バージョンの場合は、DataWeave の目次のバージョンセレクターを使用できます。

期待される型に一致しないプロパティを演算子に提供した場合、DataWeave は、自動的に、提供されたプロパティを必須の型に強制的に変換しようとします。

## [](#型強制のための-dataweave-型の定義)型強制のための DataWeave 型の定義

この DataWeave 例では、​`String`​ 型を使用して型 ​`Currency`​ を定義し、Java DecimalFormat パターン (​`##`​) を使用して値を書式設定し、​`as`​ を使用して ​`price`​ 値を ​`Currency`​ 型に強制的に変換します。

入力

```xml
<items>
    <item>
        <price>22.30</price>
    </item>
    <item>
        <price>20.31</price>
    </item>
</items>
```

DataWeave

```dataweave
%dw 2.0
output application/json
type Currency = String { format: "\$#,###.00"}
---
books: payload.items.*item map
    book:
        price: $.price as Currency
```

出力

```json
{
  "books": [
    {
      "book": {
        "price": "22.30"
      }
    },
    {
      "book": {
        "price": "20.31"
      }
    }
  ]
}
```

## [](#型強制表)型強制表

次の表は、可能な組み合わせと、変換で使用するスキーマからのプロパティを示しています。

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 25%;">
<col style="width: 50%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">ソース</th>
<th class="tableblock halign-left valign-top">対象</th>
<th class="tableblock halign-left valign-top">プロパティ</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Range</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Array</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Binary</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Binary</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Boolean</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>unit</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDate</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDate</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDate</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>unit</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Period</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Regex</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDate</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Period</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TimeZone</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Number</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code>​ / ​<code>locale</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Boolean</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Range</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>,</code>​ を区切り文字として使用して、範囲のすべての値を含む文字列を返します。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Type</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalDateTime</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>LocalTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code></p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>DateTime</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TimeZone</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>Time</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TimeZone</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>String</code>​</p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>TimeZone</code>​</p></td>
<td class="tableblock halign-left valign-top"></td>
</tr>
</tbody>
</table>

### [](#型強制のプロパティ)型強制のプロパティ

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 33.3333%;">
<col style="width: 66.6667%;">
</colgroup>
<thead>
<tr>
<th class="tableblock halign-left valign-top">プロパティ</th>
<th class="tableblock halign-left valign-top">説明</th>
</tr>
</thead>
<tbody>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>class</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Object (オブジェクト) 型の Java クラスを受け入れます。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>format</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>数値と日付を書式設定するための Java ​<code>DecimalFormat</code>​ パターンを受け入れます。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>locale</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>Java ロケールを受け入れます。Java ​<code>Locale</code>​ オブジェクトは地域 (地理的、政治的、または文化的) を表します。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>mode</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>日付値と時刻値の解析モード。有効な値: <code>SMART</code>​、​<code>STRICT</code>​、​<code>LENIENT</code>​</p>
</div>
<div class="paragraph">
<p><code>LENIENT</code>​ と ​<code>SMART</code>​ では、無効な日付の自動修正方法が異なります。また、​<code>STRICT</code>​ は、日付に対してエラーを返します。​<code>mode</code>​ を使用した​<a href="#modeExamples">例</a>​を参照してください。</p>
</div></div></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock"><code>unit</code></p></td>
<td class="tableblock halign-left valign-top"><div class="content"><div class="paragraph">
<p>値には ​<code>milliseconds</code>​ または ​<code>seconds</code>​ を使用できます。これは、Number (数値) から DateTime (日時) への変換で使用されます。</p>
</div></div></td>
</tr>
</tbody>
</table>

次の例は、​`mode`​ プロパティの使用方法を示しています。​`LENIENT`​ と ​`SMART`​ は、​`02/31/2020`​ のような無効な日付を受け取った場合でも有効な日付を返しますが、返される日付は異なります。 `STRICT`​ は、無効な日付に対してエラーを返します。例では、​`yyyy`​ ではなく ​`uuuu`​ を使用して年を表しています。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
---
{
  examples : {

    badDateWithLenient: '02/31/2020' as Date {mode: "LENIENT", format: 'MM/dd/uuuu'},
    badDateWithSmart: '02/31/2020' as Date {mode: "SMART", format: 'MM/dd/uuuu'}
  }
}
```

出力:

```json
%dw 2.0
output application/json
---
{
  "examples": {
    "badDateWithLenient": "03/02/2020",
    "badDateWithSmart": "02/29/2020"
  }
}
```

無効な日付に対して STRICT を使用するとエラーが返されます。次の例では、​`badDateWithStrict: '02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}`​ によってエラーが返されています。

```txt
"Cannot coerce String (02/31/2020) to Date, caused by: Text '02/31/2020' could not be parsed: Invalid date 'FEBRUARY 31'

9| 	badDateWithSmart:
    '02/31/2020' as Date {mode: "STRICT", format: 'MM/dd/uuuu'}
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```

次の例では、2 つの文字列を日付形式に変換し、結果を連結します。最初の文字列は日付を表し、2 番目の文字列は時刻を表します。この変換は、​`as`​ を使用して最初の文字列を ​`LocalDateTime`​ に強制変換してから、指定された形式の ​`String`​ に強制変換します。また、​`++`​ を使用して結果を連結します。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
var s1= 20201228 // (uuuuMMdd),
var s2= 1608 //(HHMM)
---
(s1 ++ s2)
as LocalDateTime {format:"uuuuMMddHHmm"}
as String {format:"MM-dd-uuuu HH:mm:ss"}
```

出力:

```json
"12-28-2020 16:08:00"
```

次の例も、文字列を日付形式に変換します。まず、この変換は ​`as`​ を使用して文字列を ​`LocalDateTime`​ 型に強制変換してから、結果を指定された出力形式の ​`String`​ 型に強制変換します。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
---
"8/30/2020 4:00:13 PM"
    as LocalDateTime {format: "M/dd/uuuu h:mm:ss a"}
    as String {format: "MM/dd/uuuu"}
```

出力:

```json
"08/30/2020"
```

次の例では、数値を 10 進数表現の文字列に変換します。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
---
{
     data: 22 as String {format: ".00"}
}
```

出力:

```json
{
  "data": "22.00"
}
```

<table><tbody><tr><td class="icon"><i class="fa icon-note" title="Note"></i></td><td class="content">DataWeave 2.5 以降、すべての形式で一貫性を維持するため、数値を直接書き込むと、書式設定が保持されず、末尾のゼロが削除されます。形式メタデータは、提供された文字列を正しい数値表現に解析するためにのみ使用されます。この動作を維持するには、​<code>stripTrailingZeroes</code>​ プロパティを使用して再度有効にします。<a href="https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-system-properties" class="xref page">DataWeave システムプロパティ</a>を参照してください。</td></tr></tbody></table>

次の例では、​`locale`​ プロパティを使用して数値と日付値を書式設定します。まず、数値を ​`String`​ 型に型強制し、Java DecimalFormat パターン (##) と ​`locale`​ プロパティ ​`en`​ (英語) または ​`es`​ (スペイン語) も使用する指定された出力形式に変換します。​`locale: "en"`​ プロパティでは、​`.`​ を使用して出力の数値の 10 進数表現を書式設定し、​`locale: "es"`​ プロパティでは、​`,`​ を使用して出力を書式設定します。  
次に、日付値を ​`Date`​ 型に型強制した後、出力形式 ​`dd-MMM-yy`​ と ​`locale`​ プロパティ ​`en`​ (英語) または ​`es`​ (スペイン語) を使用する ​`String`​ 型に型強制します。​`locale: "en"`​ プロパティでは、月 ​`MMM`​ を英語で書式設定し、​`locale: "es"`​ プロパティでは、月をスペイン語で書式設定します。

DataWeave スクリプト:

```dataweave
%dw 2.0
output application/json
---
{
    enNumber: 12.3 as String {format: "##.##", locale: "en"},
    esNumber: 12.3 as String {format: "##.##", locale: "es"},
    esDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "es"},
    enDate: "2020-12-31" as Date as String {format: "dd-MMM-yy", locale: "en"}
}
```

出力:

```json
{
  "enNumber": "12.3",
  "esNumber": "12,3",
  "esDate": "31-dic.-20",
  "enDate": "31-Dec-20"
}
```

## [](#関連情報)関連情報

- [日付と時刻の書式設定](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-cookbook-format-dates)
- [サポートされるデータ形式](https://docs.mulesoft.com/jp/dataweave/2.8/dataweave-formats)