---
schema_version: "1"
id: 4cb05668d89cdb76fe67c3b2436b42a40476fa796e70d680a32e412058924cc7
title: read
collection: mulesoft
version: current
locale: ja-jp
portal_url: https://docs.salesforce.com/mulesoft/current/ja-jp/dataweave/2.3/dw-core-functions-read.md
canonical_url: https://docs.mulesoft.com/jp/dataweave/2.3/dw-core-functions-read
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 395539d4bbc48fe04ea299b2b902bbfe4c449dbfeb2d735fe5c28ac47c9736d6
description: MuleSoft Documentation Site
release: "2.3"
latest: false
---
# read

## [](#read1)read(String | Binary, String, Object): Any

文字列またはバイナリを読み取り、解析後のコンテンツを返します。

この関数は、リーダーのデフォルトではコンテンツタイプを判別できない場合などに役立ちます。

### [](#パラメーター)パラメーター

<table class="tableblock frame-all grid-all stretch">
<colgroup>
<col style="width: 25%;">
<col style="width: 75%;">
</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>stringToParse</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">読み取る文字列またはバイナリ。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">​<code>contentType</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">サポートされる形式 (またはコンテンツタイプ)。デフォルト: <code>application/dw</code>​。</p></td>
</tr>
<tr>
<td class="tableblock halign-left valign-top"><p class="tableblock">​<code>readerProperties</code></p></td>
<td class="tableblock halign-left valign-top"><p class="tableblock">(省略可能) リーダー設定プロパティを定義します。他の形式およびリーダー設定プロパティについては、 <a href="https://docs.mulesoft.com/mule-runtime/latest/dataweave-formats">「サポートされるデータ形式」</a>​を参照してください。</p></td>
</tr>
</tbody>
</table>

### [](#例)例

次の例では、JSON オブジェクト ​`{ "hello" : "world" }'`​ を読み取り、​`"application/json"`​ 引数を使用して​_入力_​コンテンツタイプを示します。対照的に、スクリプトのヘッダーの ​`output application/xml`​ ディレクティブでは、JSON コンテンツを XML 出力に変換するようにスクリプトに指示しています。XML 出力では、​`hello`​ がルート XML 要素として使用され、​`world`​ がその要素の値として使用されています。XML の ​`hello`​ は JSON オブジェクトのキー ​`"hello"`​ に対応し、​`world`​ は JSON 値 ​`"world"`​ に対応します。

#### [](#ソース)ソース

```dw
%dw 2.0
output application/xml
---
read('{ "hello" : "world" }','application/json')
```

#### [](#出力)出力

```JSON
<?xml version='1.0' encoding='UTF-8'?><hello>world</hello>
```

### [](#例-2)例

次の例では、文字列をヘッダーなしの CSV 形式として読み取り、JSON に変換します。列名がキーとして出力オブジェクトに追加されています。また、ここでは ​`[0]`​を関数コールに付加し、結果の配列の最初のインデックスを選択しているため、結果は配列内に生成されません (出力オブジェクト全体が角括弧で囲まれています)。

#### [](#ソース-2)ソース

%dw 2.0
var myVar = "Some, Body"
output application/json
---
read(myVar,"application/csv",{header:false})\[0\]

#### [](#出力-2)出力

```JSON
{ "column_0": "Some", "column_1": " Body" }
```

### [](#例-3)例

次の例では、指定された XML を読み取り、リーダープロパティの構文 (この場合は ​`{ indexedReader: "false" }`​) を示しています。

#### [](#ソース-3)ソース

```DataWeave
%dw 2.0
output application/xml
---
{
   "XML" : read("<prices><basic>9.99</basic></prices>",
                "application/xml",
                { indexedReader: "false" })."prices"
}
```

#### [](#出力-3)出力

```XML
<?xml version='1.0' encoding='UTF-8'?>
<XML>
  <basic>9.99</basic>
</XML>
```

### [](#例-4)例

次の例では、入力パラメーターが XML または JSON 形式の場合は入力パラメーターを返し、それ以外の場合は ​`Not XML or JSON`​ メッセージを返します。 この例では ​`read`​ 関数を使用して、指定された入力文字列が XML または JSON であるかどうかを確認します。

#### [](#ソース-4)ソース

```DataWeave
%dw 2.0
import * from dw::Runtime
output application/dw
var xmlInput = "<Input><name>Max the Mule</name><ID>123</ID><Type>XML</Type></Input>"
var jsonInput = '{"Input": {"name": "Max the Mule", "ID": 123, "Type": "JSON"}}'
var stringInput = "name: Max the Mule, ID: 123, Type: String"
//This function returns the input parameter if it is in XML or JSON format, otherwise, it returns a static message
fun testType(inputMessage) =
    // Reads the input only if the data is in JSON format
    try(() -> read(inputMessage,"application/json"))
    // Reads the input only if the data is in XML format
    orElseTry(() -> read(inputMessage,"application/xml"))
    // Return this message if the input data is not either in XML or JSON format
    orElse("Not XML or JSON")
---
{
    "Test 'xmlInput' ": testType(xmlInput),
    "Test 'jsonInput' ": testType(jsonInput),
    "Test 'stringInput' ": testType(stringInput),
}
```

#### [](#出力-4)出力

```dataweave
{
  "Test 'xmlInput' ": {
    Input: {
      name: "Max the Mule",
      ID: "123",
      Type: "XML"
    }
  },
  "Test 'jsonInput' ": {
    Input: {
      name: "Max the Mule",
      ID: 123,
      Type: "JSON"
    }
  },
  "Test 'stringInput' ": "Not XML or JSON"
}
```