Webinar

November 3rd, 2021 12:00 PM EDT
Automation for Field Services & DistributionNovember 3rd, 2021 12:00 PM EDT
Learn how creating a digital workforce can improve your supply chain processes!

JSON is a library for manipulating JSON files and strings.

JSON is a common data interchange format inspired by a subset of the Javascript programming language, but these days is a de facto standard in modern web APIs and is language agnostic.

Serialization

The term serialization refers to the process of converting Robot Framework or Python types to JSON or the other way around.

Basic types can be easily converted between the domains, and the mapping is as follows:

JSON Python
object dict
array list
string str
number (int) int
number (real) float
true True
false False
null None

About JSONPath

Reading and writing values from/to JSON serializable objects is done using JSONPath. It's a syntax designed to quickly and easily refer to specific elements in a JSON structure.

Compared to Python's normal dictionary access, JSONPath expressions can target multiple elements through features such as conditionals and wildcards, which can simplify many JSON-related operations. It's analogous to XPath for XML structures.

Syntax example

For this example consider the following structure:

{
  "clients": [
    {
      "name": "Johnny Example",
      "email": "john@example.com",
      "orders": [
          {"address": "Streetroad 123", "price": 103.20},
          {"address": "Streetroad 123", "price": 98.99}
      ]
    },
    {
      "name": "Jane Example",
      "email": "jane@example.com",
      "orders": [
          {"address": "Waypath 321", "price": 22.00},
          {"address": "Streetroad 123", "price": 2330.01}
      ]
    }
  ]
}

In the simplest case JSONPath can replace nested access:

# First order of first client, with direct dictionary access
${value}=    Set variable    ${json}["clients"][0]["orders"][0]

# JSONPath access
${value}=    Get value from JSON    ${json}    $.clients[0].orders[0]

But the power comes from complicated expressions:

# Find delivery addresses for all orders
${prices}=        Get values from JSON    $..address

# Find orders that cost over 100
${expensives}=    Get values from JSON    $..orders[?(@.price>100)]

Supported Expressions

The supported syntax elements are:

Element Description
$ Root object/element
@ Current object/element
. or [] Child operator
.. Recursive descent
* Wilcard, any element
[n] Array index
[a:b:c] Array slice (start, end, step)
[a,b] Union of indices or names
?() Apply a filter expression
() Script expression

There are a multitude of different script expressions in addition to the elements listed above, which can be seen in the aforementioned article.

For further library usage examples, see the individual keywords.