# tDSQL4JSONを使用してデータを分析する

このシナリオでは、tDSQL4JSONコンポーネントを使用してJSONデータを変換および分析する標準ジョブについて説明します。

このシナリオでは、国、その都市、UTCオフセットを一覧表示するJSONデータセットが用意されています。UTCオフセットおよび都市ごとに整理し、複数のタイムゾーンにまたがる国のレポートを作成します。

以下は、このシナリオで使用するJSONデータセットのサンプルです。 ![世界各国のタイムゾーンをJSON形式で表示するサンプルデータ。](https://help.qlik.com/talend/ja-JP/components/8.0/Resources/images/tdsql4json-scenario-sample-data.png)

## 始める前に

入力用のサンプルJSONデータファイルを用意します。

## 手順

1. 標準ジョブを作成し、次のコンポーネントを追加します。
   * tFileInputRaw
   * tDSQL4JSON
   * tFileOutputRaw

   ![tDSQL4JSONコンポーネントを使用した標準ジョブ。](https://help.qlik.com/talend/ja-JP/components/8.0/Resources/images/tdsql4json-scenario-job.png)
2. tFileInputRawコンポーネントを設定します。
   1. [ファイル名]プロパティに、JSONサンプルデータの入力ファイルパスを入力します。この例では、 [world_time_zones_and_utc_offsets.json](https://help.qlik.com/talend/ja-JP/components/8.0/Content/Resources/attachments/world_time_zones_and_utc_offsets.json)になります。
   2. スキーマにJSON構造を含む単一の列が含まれていることを確認してください。
3. tDSQL4JSONコンポーネントを設定します。
   1. 複数のタイムゾーンを持つ国を抽出するには、次のスクリプトを[スクリプト]プロパティに貼り付けます。
   ```

   SELECT {
      countries = (
         // Iterate on the main input data collection.
         FROM data
   	  // Expand each country locations into individual records.
         UNNEST locations
   	  // Group location records by country name.
         GROUP BY country_name
   	  // Name this group for reference in subsequent clauses.
         GROUP AS g_country_name
   	  // Count the number of time zones per country with the use of a variable.
         LET $nbr_of_time_zones = count(country_name)
   	  // Filter to keep only countries with more than one time zone.
         HAVING $nbr_of_time_zones > 1
   	  // Sort results by number of time zones in descending order.
         ORDER BY $nbr_of_time_zones DESC
         SELECT {
            country_name,
            nbr_of_time_zones = $nbr_of_time_zones,
   		 // Build a nested list of time zones for each country.
            time_zones = (
   		    // Iterate over location records within each country group.
               FROM g_country_name
               SELECT {
                  utc_offset,
   			   // Build a nested list of cities for each time zone.
                  main_cities = (
   			      // Expand the city_list array, an implicit alias city_list is automatically defined and can be used to select items.
                     FROM city_list
                     SELECT city_list
                  )
               }
            )
         }
      )
   }
                       
   ```
   1. [サンプルデータファイル]で、入力JSONファイルを選択し、出力結果をプレビューおよび検証します。
   2. 完全なジョブを実行する前に結果を確認するには、[実行をテスト]を有効化チェックボックスを選択します。
4. tFileOutputRawコンポーネントを設定します。
   1. [ファイル名]プロパティに、処理結果を保存する場所への出力ファイルパスを入力します。
   2. 出力がJSONフィールドとして構造化されている場合は、単一列スキーマを使用します。
5. ジョブを実行します。

## タスクの結果

出力ファイルが作成され、複数のタイムゾーンを持つすべての国について、UTCオフセットと関連する都市を含む情報が記録されます。

```
{
   "countries" : [
      {
         "country_name" : "Russia",
         "nbr_of_time_zones" : 11,
         "time_zones" : [
            {
               "utc_offset" : "UTC+2",
               "main_cities" : [ "Kaliningrad" ]
            },  
            {
               "utc_offset" : "UTC+3",
               "main_cities" : [ "Kazan", "Moscow", "Nizhny Novgorod", "Rostov-on-Don", "Saint Petersburg" ]
            },
    // List of other countries with more than one time zone.
  ]
}
            
```

**関連情報**

* [tFileInputRaw](https://help.qlik.com/talend/r/ja-JP/8.0/components/raw/tfileinputraw)
* [tFileOutputRaw](https://help.qlik.com/talend/r/ja-JP/8.0/components/raw/tfileoutputraw)