July 6, 2022

Robotic Notes

All technology News

How to Read a JSON Path With Dot-Notation in Workflows – Java Code Geeks

2 min read


Okta Workflows how-to guides are questions and answers from weekly community office hours, MacAdmins Workflows Slack channel, and other places. Read all previous how-to guides.

On to the question.

How to read a JSON path with dot-notation?

As you build workflow automations it is likely you will need to read values ​​from a JSON object. For example, from an external API endpoint that returns data as JSON. Workflows has two cards to help read (retrieve) values ​​from JSON:

  • Object – Get
  • Object – Get Mutliple

We will use the following JSON to show how the cards work.

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

The dot-notation is a path that corresponds to an item in JSON.

For example, a path of product will return:

Using Object – Get card to read JSON

In Workflows, use the Object – Get to read a JSON path:

Get card for reading JSON path data

Testing this card:

Testing Get card

Let’s look at more examples.

Using the path ingredients.ingredient.0 will result in:

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

this is how it looks when testing using Object – Get card:

Using dot-notation in path

One thing to keep in mind is that the output type needs to match the JSON type. If the path you entered retrieves a plain text then the type should be Text. If the path retrieves an object then the type should be set to Object (or you will get incorrect results).

Using the above example, setting the output type to Text will result in this:

Testing Get card with incorrect output type

Setting correct output type:

Testing Get card with incorrect output type

This example shows how to access an array with ingredients.ingredient.1.type:

Accessing array using dot-notation path

Note that the output type is set to Text as the output is a string.

One more example using the path of shop.inventory will return

{
   "instock": 100,
   "ordered": 50
}
Testing Get card with correct output type

Note that the output type is set to Object as the output is an object.

Using Object – Get Multiple to read JSON

Object – Get outputs a single JSON path, Object – Get Multiple works the same and can output multiple paths. You enter any number of paths you need as output:

Using Get Multiple card

Note the output type is set on the path itself when using Object – Get Multiple card.

Testing Get Multiple card

The JSONPath Online Evaluator is an online tool where you can test your dot-notation paths.

JSONPath online evaluator tool

If you need to validate (and format) a JSON sample, use the JSON Lint tool.



Source link