# JSONスキーマを使って未加工の入力JSONデータを検証

tJSONValidatorコンポーネントを使ってJSONカラムを検証できます。

このシナリオは、Talend Data Integration、Talend Data Management Platform、Talend Big Data Platform、Talend Real-Time Big Data Platform、Talend Data Services Platform、Talend Data Fabricにのみ適用されます。

情報メモ注: tJSONValidatorコンポーネントは、Talendが提供するR2024-11以降のTalend Studioマンスリーアップデートをインストール済みである場合のみ利用できます。詳細は管理者にお問い合わせください。

## このタスクについて

未加工の入力データを使用すれば、各レコードを個別に検証できます。各レコードはスキーマに対して検証され、有効な場合は[FLOW] (フロー)リンク経由で、無効な場合は[Reject] (リジェクト)リンク経由で送信されます。

レコードをすべて検証して有効か無効かと判断したい場合は、JSONファイルを使用します。詳細は、[JSONファイルを使ってJSONスキーマを検証](https://help.qlik.com/talend/ja-JP/components/8.0/json/validating-json-schema-json-file "tJSONValidatorコンポーネントを使ってJSONカラムを検証できます。")をご覧ください。

このシナリオのジョブでは、以下のコンポーネントを使用しています:

* 未加工の入力データを使用するためのtFixedFlowInputコンポーネント。
* JSONカラムを検証するためのtJSONValidatorコンポーネント。
* 結果を出力するためのtLogRowコンポーネント。

![tFixedFlowInputコンポーネント、tJSONValidatorコンポーネント、2つのtLogRowコンポーネントを使用したジョブ。](https://help.qlik.com/talend/ja-JP/components/8.0/Resources/images/tjsonvalidator_job.png)

## ジョブを設定

### 手順

1. [Palette] (パレット)から、tFixedFlowInput、tJSONValidator、2つのtLogRowをデザインワークスペースにドロップします。
2. [Main] (メイン)リンクを使って、tFixedFlowInputをtJSONValidatorに接続させます。
3. [FLOW] (フロー)リンクを使って、tJSONValidatorを1つのtLogRowに接続させます。
4. [Reject] (リジェクト)リンクを使って、tJSONValidatorをもう1つのtLogRowに接続させます。

## 入力コンポーネントを設定

### 手順

1. tFixedFlowInputコンポーネントをダブルクリックして、[Component] (コンポーネント)タブで[Basic settings] (基本設定)ビューを開きます。
2. [Edit Schema] (スキーマを編集)をクリックします。
3. 開くダイアログボックスで[+]ボタンをクリックし、初期入力データを保持するカラムを追加します。

   この例では、次の2つのカラムを追加します:
   * idを追加し、タイプとしてIntegerを選択します。
   * jsonを追加し、タイプとしてStringを選択します。
4. OKをクリックしてダイアログボックスを閉じます。
5. [Use Inline Content] (インラインコンテンツを使用)を選択します。
6. [Content] (コンテンツ)に入力データを入力します。

   このサンプルでは、次のようになります。
   ```
   1;{"customers":[{"lastName":"Smith","firstName":"Jane","address":{"street":"4876 Felosa Drive","postCode":90017,"city":"Los Angeles","state":"CA","country":"United States"}}]}
   2;{"customers":[{"lastName":"Doe","firstName":"John","address":{"street":"42373 Tenmile","postCode":92203,"city":"Bermuda Dunes","state":"CA","country":"United States"}}]}
   3;{"customers":[{"lastName":"Frye","firstName":"Lucy","address":{"street":"4862 Parkway Street","postCode":"92298","city":"Bermuda Dunes","state":"CA","country":"United States"}}]}
   4;{"customers":[{"lastName":"Joney","firstName":"Seb","address":{"street":"386 Hart Country Lane","postCode":30830,"city":"Waynesboro","state":8,"country":"United States"}}]}
   5;{"customers":[{"lastName":"Ming","firstName":"Li","address":{"street":"321 Da Bei Yao","postCode":10012,"city":"Bei Jing","state":"","country":"China"}}]}
   ```

## tJSONValidatorコンポーネントを設定

### 手順

1. tJSONValidatorをダブルクリックし、その[Basic settings] (基本設定)ビューを表示させて、コンポーネントのプロパティを定義します。
2. 必要な場合は、[Sync columns] (カラムを同期)タブをクリックし、入力コンポーネントで定義されているスキーマを取得します。
3. ![\[Add\] (追加)](https://help.qlik.com/talend/ja-JP/components/8.0/Resources/images/add.svg)をクリックし、[Allocate] (割り当て)エリアに行を追加します。
4. ドロップダウンリストから入力カラムを選択します。
5. スキーマファイルへのパスを二重引用符で囲んで入力し、データを検証するスキーマを定義します。

   この例では、"inputSchema.json"です。 ![tJSONValidatorの設定](https://help.qlik.com/talend/ja-JP/components/8.0/Resources/images/tjsonvalidator-jobs.png)

   情報メモ注: スキーマで$schemaダイアレクトが定義されていない場合は、[Advanced settings] (詳細設定)で[JSON schema draft version] (JSONスキーマドラフトバージョン)を定義します。

## 出力コンポーネントの設定とジョブの実行

### 手順

1. tLogRowコンポーネントをダブルクリックして、[Basic settings] (基本設定)ビューを表示させます。
2. どちらのコンポーネントにも[Table (print values in cells of a table)] (テーブル(テーブルのセルの出力値))を選択します。
3. F6を押すと、ジョブを保存して実行します。

### タスクの結果

有効なレコードはそれぞれ[FLOW] (フロー)リンク経由で送信され、無効なレコードはそれぞれ[Reject] (リジェクト)リンク経由で送信されます。

```
.--+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
|                                                                                   tLogRow_3(OK)                                                                                  |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|id|jsonValue                                                                                                                                                                    |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|1 |{"customers":[{"lastName":"Smith","firstName":"Jane","address":{"street":"4876 Felosa Drive","postCode":90017,"city":"Los Angeles","state":"CA","country":"United States"}}]}|
|2 |{"customers":[{"lastName":"Doe","firstName":"John","address":{"street":"42373 Tenmile","postCode":92203,"city":"Bermuda Dunes","state":"CA","country":"United States"}}]}    |
|5 |{"customers":[{"lastName":"Ming","firstName":"Li","address":{"street":"321 Da Bei Yao","postCode":10012,"city":"Bei Jing","state":"","country":"China"}}]}                   |
'--+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------'

.--+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------.
|                                                                                                                                                                                                                                                                                                                     tLogRow_4(NOK)                                                                                                                                                                                                                                                                                                                      |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|id|jsonValue                                                                                                                                                                         |ExecutionResult                                                                                                                                                                                                                                                                                                                                                                                                                                               |
|=-+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------=|
|3 |{"customers":[{"lastName":"Frye","firstName":"Lucy","address":{"street":"4862 Parkway Street","postCode":"92298","city":"Bermuda Dunes","state":"CA","country":"United States"}}]}|{"ExecutionResult":{"Status":"Invalid","Validations":[{"ColumnName":"jsonValue","Violations":[{"SchemaLocation":"https://example.com/arrays.schema.json#/$defs/customer/properties/address/properties/postCode/type","ErrorCode":"1029","ErrorType":"type","ErrorMessage":"$.customers[0].address.postCode: string found, integer expected","ElementValue":"92298","ElementLocation":"$.customers[0].address.postCode"}],"SchemaURI":"C:/inputSchema.json"}]}}|
|4 |{"customers":[{"lastName":"Joney","firstName":"Seb","address":{"street":"386 Hart Country Lane","postCode":30830,"city":"Waynesboro","state":8,"country":"United States"}}]}      |{"ExecutionResult":{"Status":"Invalid","Validations":[{"ColumnName":"jsonValue","Violations":[{"SchemaLocation":"https://example.com/arrays.schema.json#/$defs/customer/properties/address/properties/state/type","ErrorCode":"1029","ErrorType":"type","ErrorMessage":"$.customers[0].address.state: integer found, string expected","ElementLocation":"$.customers[0].address.state"}],"SchemaURI":"C:/inputSchema.json"}]}}                                |
'--+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'
```

**関連資料**

* [tJSONValidator](https://help.qlik.com/talend/ja-JP/components/8.0/json/tjsonvalidator "特定のJSONスキーマに基づいてデータを検証します。")

**関連情報**

* [tLogRow](https://help.qlik.com/talend/r/ja-JP/8.0/components/logs-and-errors/tlogrow)
* [tFixedFlowInput](https://help.qlik.com/talend/r/ja-JP/8.0/components/tfixedflowinput/tfixedflowinput)