開発者コンソール

JSONでのクエリの実行

JSONでのクエリの実行

レシピの構成では、Jayway JsonPath構文を使用してqueryパラメーターを作成しました。JSONクエリ構文の詳細は、こちらの Jayway JsonPath(英語のみ)で確認できます。

Jayway JsonPath Evaluator(英語のみ)を使用すると、クエリ構文の動作をよりよく理解できます。特に、さまざまな構文($、@、*、..、.<name>、[?(<expression>)])によってJSONの特定の要素をターゲットにする方法を理解するには、Operators(英語のみ)のセクションを参照してください。

以下のセクションでは、Jayway JsonPathクエリの作成方法に関する例を紹介します。

JSON Pathクエリの例1

この例では、いくつかのJayway JsonPathクエリを、順を追って詳しく見ていきます。

  1. urlFile.jsonapp > assets内)を開きます。
  2. 最初のURLをコピーします。

    http://www.lightcast.com/api/firetv/channels.php?app_id=257&app_key=0ojbgtfcsq12&action=channels_videos
    
  3. このURLをブラウザに貼り付け、Enterキーを押してコンテンツを表示します。
  4. コンテンツをコピーし、jsonprettyprint.com(英語のみ)などのツールを使用してJSONを整形します。
  5. 整形されたJSONをコピーし、Jayway JsonPath Evaluator(英語のみ)に貼り付けます。
  6. [Go!] ボックスに、すべてのカテゴリーを取得する次のクエリを入力します。

    $..categories[*]
    
  7. [Go!] をクリックします。

    フィードのすべてのカテゴリーが結果に表示されます。

    [
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Underwater",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Attractions",
       "Bahamas Islands",
       "Bahamas Attractions",
       "Bahamas Underwater",
       "Bahamas Islands",
       "Bahamas Attractions",
       "Bahamas Islands",
       "Bahamas Islands",
       "Bahamas Attractions",
       "Bahamas Weddings",
       "Bahamas Weddings",
       "Bahamas Attractions",
       "Bahamas Underwater",
       "Bahamas Islands",
       "Bahamas Attractions",
       "Bahamas Islands",
       "Bahamas Weddings",
       "Bahamas Islands",
       "Bahamas Underwater"
    ]
    

    このフィードでは、各メディアオブジェクトにカテゴリーが設定されているため、クエリの結果に多数の「Bahamas Islands」が(このカテゴリーを含むメディアオブジェクトごとに1つずつ)返されます。

    JsonPath Evaluatorの例
    Jayway JsonPathクエリ構文を使用してフィードからすべてのカテゴリーを抽出します。

    構文$..categories[*]は、ルートディレクトリ($)から開始して、すべてのサブディレクトリ(..)でcategoriesというキーを検索します。このキーは配列([])を含んでいる必要がありますが、配列内の要素は何でも構いません(*)。

    クエリを$..title[*]に変更すると、titleはフィード内で配列を含むキーではないため、何も一致しなくなります。すべてのタイトルを取得するには、$..titleを使用します。

    Fire App Builderのコードでは、DynamicParserクラスにより、これらのカテゴリーをマップに追加するときに重複が削除されます。

JSON Pathクエリの例2

メディアのフィード構造の例をもう1つ紹介します。

{  
   "total":"13",
   "results":[  
      {  
         "id":"45454333",
         "title":"'サンプルビデオタイトル1",
         "link":"http:\/\/example.com\/assets\/some\/media\/path\/myvideo1.mp4",
         "pubDate":"Tue, 01 Oct 2016 18:29:52 +0530",
         "media:thumbnail":"http:\/\/example.com\/assets\/some\/someimage1" type="jpg",
         "media:duration":"1913",
         "description":"サンプルの説明1",
         "media:filepath":"http:\/\/example.com\/videos\/1234\/myvideo1.mp4",
         "media:fullimage":"http:\/\/example.com\/videos\/1234\/thumbnail1" type="jpg",
         "media:keywords":"例, サンプル, キーワード, ランダム"
      },
      {  
         "id":"44545333",
         "title":"サンプルビデオタイトル2",
         "link":"http:\/\/example.com\/assets\/some\/media\/path\/myvideo2.mp4",
         "pubDate":"Tue, 01 Sep 2016 18:29:52 +0530",
         "media:thumbnail":"http:\/\/example.com\/assets\/some\/someimage2" type="jpg",
         "media:duration":"1945",
         "description":"サンプルの説明2",
         "media:filepath":"http:\/\/example.com\/videos\/1234\/myvideo2.mp4",
         "media:fullimage":"http:\/\/example.com\/videos\/1234\/thumbnail2" type="jpg",
         "media:keywords":"例, サンプル, キーワード, ランダム"
      },
           {  
         "id":"4543434333",
         "title":"サンプルビデオタイトル3",
         "link":"http:\/\/example.com\/assets\/some\/media\/path\/myvideo3.mp4",
         "pubDate":"Tue, 01 Aug 2016 18:29:52 +0530",
         "media:thumbnail":"http:\/\/example.com\/assets\/some\/someimage3" type="jpg",
         "media:duration":"1955",
         "description":"サンプルの説明",
         "media:filepath":"http:\/\/example.com\/videos\/1234\/myvideo3.mp4",
         "media:fullimage":"http:\/\/example.com\/videos\/1234\/thumbnail3" type="jpg",
         "media:keywords":"例, サンプル, キーワード, ランダム"
      }

   ]
}

ここで、すべてのアイテムからmedia:thumbnailの値を選択する必要があるとすると、次の構文を使用できます。

$..results[*].media:thumbnail

この構文は、ルートから開始してすべてのアイテムを再帰的に検索し、results配列内のすべての要素を照合して、media:thumbnailを選択します。次の結果が返されます。

[
   "http://example.com/assets/some/someimage1" type="jpg",
   "http://example.com/assets/some/someimage2" type="jpg",
   "http://example.com/assets/some/someimage3" type="jpg"
]

JSON Pathクエリの例3

この例では、Fire App Builderからのフィードを使用する代わりに、より複雑なフィード構造からコンテンツをキャプチャします。

  1. 次のフィードをコピーし、Jayway JsonPath Evaluator(英語のみ)に入力します。

    {
    "brand": "myChannel",
      "version": "2.0",
      "creationDate": 1462590933,
    
        "containers": [
            {
                "type": "movie.Container",
                "subtype": "movies",
                "common": {
                    "title": "映画"
                },
                "movies": [
                    {
                        "subtype": "movie",
                        "cat": "恋愛",
                        "availableDate": 1351631300,
                        "expirationDate": 1364764900,
                        "common": {
                            "authId": "1234",
                            "title": "サンプル映画タイトル",
                            "subtitle": "映画のサンプル字幕",
                            "description": "これは映画の説明です。基本的なプロットとストーリーを紹介します。",
                            "tags": [
                                "サンプル",
                                "タグ",
                                "設定"
                            ],
                            "promotionalText": "",
                            "theshareURL": "http://yourcompany.com/mymovieurl1",
                            "imageUrls": {
                                "1000x500_portal": "http://www.yourcompany.com/moviethumbl" type="jpg"
                            }
                        }
                    }
                ]
            },
    
            {
                "type": "movie.Container",
                "subtype": "movies",
                "common": {
                    "title": "映画"
                },
                "movies": [
                    {
                        "subtype": "movie",
                        "cat": "アドベンチャー",
                        "availableDate": 1351631400,
                        "expirationDate": 1364765900,
                        "common": {
                            "authId": "5678",
                            "title": "サンプル映画タイトル2",
                            "subtitle": "英語のサンプル字幕2",
                            "description": "これは別のサンプル映画タイトルです。",
                            "tags": [
                                "サンプル",
                                "タグ",
                                "設定"
                            ],
                            "promotionalText": "",
                            "theshareURL": "http://yourcompany.com/mymovieurl2",
                            "imageUrls": {
                                "1000x500_portal": "http://www.yourcompany.com/moviethumb2" type="jpg"
                            }
                        }
                    }
                ]
            }
        ]
    }
    
  2. [Go!] ボックスに次のように入力します。

    $.containers[?(@.type == 'movie.Container')].movies[*].cat
    

    このクエリの意味は次のとおりです。

    クエリ構文 意味
    $ ルートレベルから開始します。
    .containers[ ルートレベルのキー「containers」のうち、(特定のコンテンツの)配列を含むすべてのインスタンスに一致します。
    ?(@.type == 'movie.Container')]: キーtypemovie.Containerに等しいという条件に一致するフィルターを作成します。  
    .movies[*] movies配列内のすべてのコンテンツに一致します。
    .cat movies配列内のcatに一致します。
  3. [Go!] をクリックします。

    結果には、次のようにcatが取得されます。

     [
        "恋愛",
        "アドベンチャー"
     ]
    

例4

次のようなフィード構造があるとします。

[
  {
    "ChannelLang": "日本語",
    "VideoId": "239870",
    "VidTitle": "トップストーリー",
    "videoKeywords": "ニュース, ストーリー, 犯罪,",
    "videoDescription": "これはビデオの説明です。",
    "slug": "top-news-stories",
    "duration": 4318,
    "image": "http:\/\/example.com\/2016\/11\/43434" type="jpg",
    "thumb": "http:\/\/example.com\/2016\/11\/43434_thumb" type="jpg",
    "vidUrl": "http:\/\/example.com\/2016\/10\/myvideo.mp4",
    "created": "2016-11-02 23:08:42",
    "vidcategory": "アドベンチャー"
  },
  {
    "ChannelLang": "日本語",
    "VideoId": "239870",
    "VidTitle": "トップストーリー",
    "videoKeywords": "ニュース, ストーリー, 犯罪,",
    "videoDescription": "これはビデオの説明です。",
    "slug": "top-news-stories",
    "duration": 4318,
    "image": "http:\/\/example.com\/2016\/11\/43434" type="jpg",
    "thumb": "http:\/\/example.com\/2016\/11\/43434_thumb" type="jpg",
    "vidUrl": "http:\/\/example.com\/2016\/10\/myvideo.mp4",
    "created": "2016-11-02 23:08:42",
    "vidcategory": "アドベンチャー"
  },
   {
     "ChannelLang": "日本語",
     "VideoId": "239870",
     "VidTitle": "トップストーリー",
     "videoKeywords": "ニュース, ストーリー, 犯罪,",
     "videoDescription": "これはビデオの説明です。",
     "slug": "top-news-stories",
     "duration": 4318,
     "image": "http:\/\/example.com\/2016\/11\/43434" type="jpg",
     "thumb": "http:\/\/example.com\/2016\/11\/43434_thumb" type="jpg",
     "vidUrl": "http:\/\/example.com\/2016\/10\/myvideo.mp4",
     "created": "2016-11-02 23:08:42",
     "vidcategory": "アドベンチャー"
   }
]

次のクエリを使用すると、カテゴリーをターゲットにすることができます。

$..vidcategory