Okta Workflows How-To:ドット表記でJSONパスを読み取る

作者について

Max Katz

Staff Developer Advocate, Okta Workflows

Max Katz is a Staff Developer Advocate for Okta Workflows. Max loves the no-code automation space and creating educational content. Before Okta, Max led the North America Developer Advocacy team at IBM. Before IBM, Max led developer advocacy for a cloud-based mobile app platform, Appery, helping it grow from zero to over 400,000 developers. Max regularly writes on his http://maxkatz.net blog. 

28 10月 2022 読了目安時間: ~

A person is seated at a desk working on a computer in a modern office environment

Okta Workflowsは、アイデンティティプロセスを自動化するためのノーコードプラットフォームです。Workflowsチームは、ビルダーからの質問に回答するオフィスアワーを毎週開催しています。

このブログ記事は、オフィスアワーで寄せられた質問「Workflowsでドット表記のJSONパスを読み取る方法」を基に作成されています。

はじめに

Workflowsの自動化を構築する際には、JSONオブジェクトから値を読み取る必要が生じることがよくあります。たとえば、JSON形式でデータを返す外部APIエンドポイントから取得する場合などです。Workflowsには、JSONから値を読み取る(取得する)ためのカードが2つあります。

  • Object – Get
  • Object – Get Multiple

以下のJSONを使用して、カードの動作を示します。

{
    "product": "ice cream",
    "ingredients": {
        "ingredient": [{
                "id": 100,
                "type": "chocolate"
            },
            {
                "id": 200,
                "type": "vanilla"
            },
            {
                "id": 300,
                "type": "caramel"
            }
        ]
    },
    "shop": {
        "inventory": {
            "instock": 100,
            "ordered": 50
        }
    }
}

ドット表記は、JSON内の項目に対応するパスです。

例えば、パスがproductの場合は次が返されます。

ice cream

Object – Getカードを使用してJSONを読み取る

Workflowsでは、Object – Getを使用してJSONパスを読み取ります。

Okta Workflows Object Get card displaying an input object with product and ingredients JSON, a path set to product, and an empty output field. Getカードを使用してJSONパスデータを読み取る

このカードをテストします。

Test "Get" modal displaying an input JSON object, path notation set to product, and evaluated output string "ice cream". Getカードのテスト

他の例も見てみましょう。

パスにingredients.ingredient.0を使用します。結果は次のとおりです。

{
  "id": 100,
  "type": "chocolate"
}

Object – Getカードを使用してテストした場合の結果:

Interface for testing a JSON path with dot notation, showing input JSON, path ingredients.ingredient.0, and output JSON. パスでのドット表記の使用

出力タイプはJSONタイプと一致させる必要がある点に注意してください。入力したパスでプレーンテキストが取得される場合、タイプはTextに設定する必要があります。パスがオブジェクトを取得する場合、タイプをObjectに設定する必要があります(設定しないと、誤った結果が返されます)。

上記の例を使用し、出力タイプをTextに設定すると、次のようになります。

Test "Get" interface showing a JSON input object, path notation ingredients.ingredient.0, and output displaying [object Object]. 不適切な出力タイプでのGetカードのテスト

正しい出力タイプの設定:

Okta Workflows Object Get card configured with path ingredients.ingredient.0 and an orange arrow pointing to the output field labeled "Enter key name here". 正しい出力タイプでのGetカードのテスト

この例では、ingredients.ingredient.1.typeを使用して配列にアクセスする方法を示します。

Test "Get" modal displaying an array with chocolate and vanilla objects, path ingredients.ingredient.1.type, and output value "vanilla". ドット表記パスを使用した配列へのアクセス

出力が文字列であるため、出力タイプはTextに設定されています。

shop.inventoryのパスを使用したもう1つの例では、次の値が返されます。

{
   "instock": 100,
   "ordered": 50
}
Test "Get" modal displaying an input object with shop inventory JSON, path shop.inventory, and output JSON showing instock and ordered counts. Getカードを使ってJSONオブジェクトにアクセスするテスト

出力はオブジェクトであるため、出力タイプはObjectに設定されています。

Object – Get Multipleを使用してJSONを読み取る

Object – Getは、単一のJSONパスを出力します。Object – Get Multipleカードも同様に機能し、複数のパスを出力できます。出力として必要なパスを任意の数だけ入力します。

Okta Workflows Object Get Multiple card showing JSON input data, multiple path inputs including product and shop.inventory, and an orange arrow pointing to a selected path field. Get Multipleカードの使用方法

Object – Get Multipleカードを使用する場合、出力タイプは指定する各パスの項目ごとに個別に設定する必要があります。

Test "Get Multiple" modal displaying input JSON and corresponding outputs for paths product, ingredients.ingredient.0, ingredients.ingredient.1.type, and shop.inventory. Get Multipleカードのテスト

JSONPath Online Evaluatorは、ドット表記のパスをテストできるオンラインツールです。

JSONPath Online Evaluator web interface showing input JSON, JSONPath shop.inventory, and evaluated results displaying instock and ordered counts. JSONPath Online Evaluator

JSONサンプルの検証(およびフォーマット)が必要な場合は、JSON Lintツールを使用してください。

Workflowsに関するご質問にお答えします

Okta Workflowsについてご質問はありますか?フローの作成方法がわかりませんか?毎週開催されるコミュニティのオフィスアワーにご参加ください。サポートが受けられます。

作者について

Max Katz

Staff Developer Advocate, Okta Workflows

Max Katz is a Staff Developer Advocate for Okta Workflows. Max loves the no-code automation space and creating educational content. Before Okta, Max led the North America Developer Advocacy team at IBM. Before IBM, Max led developer advocacy for a cloud-based mobile app platform, Appery, helping it grow from zero to over 400,000 developers. Max regularly writes on his http://maxkatz.net blog. 

アイデンティティ施策を推進