Source Path Expressions

Overview

Verifications and Set Variable Steps both allow the user to specify a Source. The Source always references a Variable which should be used as the source of the value to be used for verification or extraction. If the Source is a complex value (like a JSON or XML) and the user needs to use just a part of that value, one can use the Source Path field to extract the desired parts of the Source.

Test Studio for APIs supports JSONPath and XPath for path expressions.

To get the entire content of the Source, just leave the Path field empty.

XPath

Since version R1 2017, Test Studio for APIs supports XPath expressions. Here are some useful links where you can read more about the XPath syntax:

JSONPath

Test Studio for APIs supports basic JSONPath expressions. The original article, describing the JSONPath specification can be found here. Note though that different JSONPath implementations cover that specification to a different extend and variation. Test Studio for APIs supports most but not all of the expressions described in the JSONPath specification. See Current JSONPath Limitations below for more information.

See below for some examples.

Current JSONPath Limitations

There are certain limitations of the scenarios that can be achieved with JSONPath in Test Studio for APIs. The Test Studio team is working hard to provide implementations or alternatives for them.

  • The length of a JSON array cannot be retrieved using JSONPath (e.g.: $.users.length()). Currently Test Studio for APIs does not provide a mechanism to extract the total length of a JSON array.
  • Test Studio for APIs currently does not support replacing property names or indexes with script expressions. For example such an expression would be: $.store.book[(@.length-1)]. A supported alternative of the last expression would be: $.store.book[-1:].

Examples

As an example of the JSONPath expressions that are supported in Test Studio for APIs we will use the same book store example as provided in the JSONPath specification.

{ 
    "store": {
        "book": [ 
            { "category": "reference",
                "author": "Nigel Rees",
                "title": "Sayings of the Century",
                "price": 8.95
            },
            { "category": "fiction",
                "author": "Evelyn Waugh",
                "title": "Sword of Honour",
                "price": 12.99
            },
            { "category": "fiction",
                "author": "Herman Melville",
                "title": "Moby Dick",
                "isbn": "0-553-21311-3",
                "price": 8.99
            },
            { "category": "fiction",
                "author": "J. R. R. Tolkien",
                "title": "The Lord of the Rings",
                "isbn": "0-395-19395-8",
                "price": 22.99
            }
        ],
        "bicycle": {
            "color": "red",
            "price": 19.95
        }
    }
}
XPath JSONPath Result
/store/book/author $.store.book[*].author the authors of all books in the store
//author $..author all authors
/store/* $.store.* all things in store
/store//price $.store..price the price of everything in the store
//book[3] $..book[2] the third book
//book[last()] $..book[-1:] the last book in order (note that the "$..book[(@.length-1)]" expression is not supported in Test Studio)
//book[position()<3] $..book[0,1]
$..book[:2]
the first two books
//book[isbn] $..book[?(@.isbn)] filter all books with isbn number
//book[price<10] $..book[?(@.price<10)] filter all books cheaper than 10
//* $..* all Elements in XML document. All members of JSON structure

Check the Sample Project for more real-life examples of path expressions.