Guide d’utilisation des Okta Workflows : lire un chemin JSON avec la notation par points

À propos de l’auteur

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 octobre 2022 Temps de lecture: ~

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

Okta Workflows est une plateforme no-code permettant d’automatiser les processus liés à l’identité. L’équipe Workflows organise chaque semaine des permanences au cours desquelles elle répond aux questions des personnes qui créent des workflows.

Cet article de blog s’appuie sur une question posée lors des permanences : comment lire un chemin JSON avec la notation par points dans Workflows ? 

Premiers pas

Lorsque vous créez des automatisations Workflows, vous aurez probablement besoin de lire des valeurs à partir d’un objet JSON. Par exemple, à partir d’un API endpoint externe qui renvoie des données au format JSON. Les Workflows disposent de deux cartes permettant de lire (retrieve) des valeurs à partir d’un objet JSON :

  • Objet – obtenir
  • Objet – Get Multiple

Utilisez le JSON suivant pour illustrer le fonctionnement des cartes. 

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

La notation pointée correspond à un chemin qui correspond à un élément dans JSON. 

Par exemple, un chemin de produit renverra :

glace

Utiliser la carte Object – Get pour lire le JSON

Dans Workflows, utilisez la fonction Object – Get pour lire un chemin 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. Utiliser la carte Get pour extraire des données de chemin JSON

Test de cette carte :

Test "Get" modal displaying an input JSON object, path notation set to product, and evaluated output string "ice cream". Test de la carte Get

Penchons-nous sur d’autres exemples : 

En utilisant le chemin ingredients.ingredient.0 entraînera :

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

Résultat lors du test avec la carte Object – Get : 

Interface for testing a JSON path with dot notation, showing input JSON, path ingredients.ingredient.0, and output JSON. Utilisation de la notation pointée dans le chemin d’accès

Un point important à retenir : le type de sortie doit correspondre au type JSON. Si le chemin que vous avez saisi extrait du texte ordinaire, le type doit être Texte. Si le chemin extrait un objet, le type doit alors être défini sur Object (sinon, vous obtiendrez des résultats incorrects). 

En reprenant l’exemple ci-dessus, si vous définissez le type de sortie sur Texte, vous obtiendrez le résultat suivant : 

Test "Get" interface showing a JSON input object, path notation ingredients.ingredient.0, and output displaying [object Object]. Test du composant Get avec un type de sortie incorrect

Définition du type de sortie approprié : 

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". Test du composant Get avec un type de sortie correct

Cet exemple montre comment accéder à un tableau avec ingredients.ingredient.1.type:

Test "Get" modal displaying an array with chocolate and vanilla objects, path ingredients.ingredient.1.type, and output value "vanilla". Accéder à un tableau à l’aide d’un chemin en notation pointée

Veuillez noter que le type de sortie est défini sur Texte, car la sortie correspond à une chaîne de caractères. 

Un autre exemple utilisant le chemin shop.inventory renverra

{
   "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. Test du composant Get pour accéder à un objet JSON

Notez que le type de sortie est défini sur Object, car la sortie est un objet. 

Utiliser Object – Get Multiple pour extraire du JSON

Object – Get génère un seul chemin JSON. La carte Object – Get Multiple fonctionne de manière similaire et peut produire plusieurs chemins. Vous saisissez autant de chemins de sortie que nécessaire :

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. Utilisation de la carte Get Multiple

Notez que le type de sortie est défini dans le chemin lors de l’utilisation de la carte 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. Test de la carte Get Multiple

L’ outil d’évaluation JSONPath en ligne est un outil en ligne qui vous permet de tester vos chemins en notation pointée. 

JSONPath Online Evaluator web interface showing input JSON, JSONPath shop.inventory, and evaluated results displaying instock and ordered counts. Outil d’évaluation JSONPath en ligne

Si vous devez valider (et formater) un exemple JSON, utilisez l’outil JSON Lint.

Obtenez des réponses à vos questions sur les Workflows

Avez-vous une question concernant Okta Workflows ? Vous ne savez pas comment créer un flux ? Participez aux permanences hebdomadaires de la communauté pour obtenir de l’aide. 

À propos de l’auteur

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. 

Continuez votre parcours dans l‘univers de l’identité