JSON extraction using JSONPath

Document created by Chris Sommerstad Employee on Jul 21, 2017
Version 1Show Document
  • View in full screen mode

CloudTest provides support for extracting values from JSON document responses using JSONPath. JSONPath UI options are found in the Message Editor, in the Session Template Package Wizards's Clip Scanner, or while using the Session Template Wizard. Additionally, scripts can use two new JSONPath Message properties via the getResponse method: RESPONSE_HTTP_BODY_WITH_JSONPATH and RESPONSE_TEXT_WITH_JSONPATH.

Support using JSONPath resolves limitations imposed by XML that are not limitations of JSON itself including use of whitespace, special characters, and numbers. Since JSONPath is designed for JSON, and not XML, CloudTest is capable of producing much better extractions using JSONPath.

TIP:
       Users new to JSONPath may wish to refer to the following resources:

 

The examples in this article uses the following JSON document example as response text.

{ "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,
         "isbn": "0-553-21311-3"
     }
    ],
         "bicycle": {
         "color": "red",
         "price": 19.95
    }
  }
}

JSONPath examples using the JSON Document above:

$.store.book[5].title
$.store.book[?(@.price < 10)].title

JSONPath UI options are found in the Message Editor, in the Session Template Package Wizards's Clip Scanner, or while using the Session Template Wizard. Additionally, scripts can use two new JSONPath options in the getResponse method.

Note:  The attributes JSONPath and XPath are mutually exclusive. 

JSONPath Extraction and Clip Custom Properties

In a test clip based on the same sample JSON document above, we can manually set up a simple property set using JSONPath and the constant value $.store.book[*].author.

  1. Add a Property Set form as you would for any message where you'll refer to an existing clip custom property or where you'll create a new one.
  2. Specify JSONPath as the retrieval method. When you do so, the Validations form changes to add a JSONPath field.

In the example below, the JSONPath is supplied from an existing clip custom property named in the Property Path field, which has a constant value of $.store.book[*].author.

Note:  If we were to eliminate the clip custom property and provide its value (e.g. as $.store.book[*].author) we would get a match on the message played response.

In this case, having the clip custom property allows us to manipulate the data in other ways. Toggle the blue linked text, Hide jsonpath, to show/hide the underlying path.

JSONPath Extraction and Validation

In a test clip based on the JSON document above (refer to JSON Document for more information about this example), we can easily set up a validation on our document using JSONPath and the Message Editor's Validations form.

  1. Add the Validations form as you would for any message-based validation.
  2. Specify JSON as the response type. When you do so, the Validations form changes to add a JSONPath field.

TIP:  Not all JSON documents have a response type of JSON. Use the format suitable for the content you are testing.

 

  1. Specify the JSONPath to the response text to extract. For example, $.store.book[*].author.

 

  1. Specify the Match Type. In the shown example, we are using Matches exactly and then specifying the response text to extract (e.g. Nigel Rees) as a Constant expression.

JSONPath in Scripts

JSONPath extraction can be performed by script using either or both of the following new response properties with JSONPath.

RESPONSE_HTTP_BODY_WITH_JSONPATH 

 

This constant can be passed as the "indicator" parameter in calls to the "getResponse" method. 

 

Parses the body of the HTTP response received as JSON, evaluates a JSONPath expression for that JSON, and returns the result of that JSONPath expression. The "param" parameter contains the JSONPath expression. This indicator value is valid only for responses that are HTTP-based. If no items in the JSON match, null is returned. If there are one or more matching nodes, a string array is returned. (An array is returned even if there is only one match.)

For example, the example script (shown above) uses a test clip based on the sample JSON document found here. Using previousItem, it gets a portion of the response with JSONPath (e.g. books with a price greater than 8):

var msg1 = $context.currentItem.previousItem;
var matches = msg1.getResponse(msg1.RESPONSE_HTTP_BODY_WITH_JSONPATH, "$.store.book[?(@.price > 8)]");

The same result can be achieved using the second property, which gets the same value (e.g. $.store.book[?(@.price > 8) response text using JSONPath.

RESPONSE_TEXT_WITH_JSONPATH

 

This constant can be passed as the "indicator" parameter in calls to the "getResponse" method.

 

Processes the body of the HTTP response assuming it is JSON, evaluates a JSONPath expression for that JSON, and returns the result of that JSONPath expression. The "param" parameter contains the JSONPath expression. This indicator value is valid only for responses that are HTTP-based. If no items in the JSON match, null is returned. If there are one or more matching nodes, a string array is returned. (An array is returned even if there is only one match.)

For example, in a test clip using the sample JSON document, retrieving the same value as the prior example:

matches = msg1.getResponse(msg1.RESPONSE_TEXT_WITH_JSONPATH, "$.store.book[?(@.price > 8)]");

 

JSONPath and Custom Property Sets

It is possible to set up much more complex Property Sets using JSONPath extraction and a script that retrieves elements from an array created from the response using property sets. In such a test clip, a script retrieves values from an array created using the property sets (with values from the sample JSON response) shown below.

In propertySet-0, shown below, ClipProp1 will retrieve the value of the first book's title, Sayings of the Century (e.g. $.store.book[0].title).

The result for ClipProp1 is shown below in the Result Details, Custom Properties section.

Similarly, in propertySet-1, Test on Played Response shows the outcome, Nigel Rees (e.g. the author of Sayings of the Century). In this custom property, the Match Options uses all value(s) in tandem with a slightly different JSONPath than we've used to get this author in several prior examples (e.g. in this case rather than $.store.book[*].author in tandem with Matches exactly we are using  $.store.book[0].author—the author for the first book in the given array).

JSONPath Extraction in the Session Template Package Wizard

The Session Template Package Wizard now supports JSONPath as a Value Retrieval Method and will designate that method, where appropriate. This value retrieval method can also be manually selected where JSONPath is a relevant option.

Where JSONPath is the assigned Value Retrieval Method, click the Edit icon to access the JSONPath Parser. This dialog box shows the precise JSONPath string with the suggested extraction highlighted in orange.

Similarly, JSONPath can be specified as the Retrieval Method while using the Session Template Wizard on selected text.

JSONPath in Result Details

JSON Response Text is now presented within the given XML Response Text tag in JSONPath format, whereas in releases prior to SOASTA 51, an XML representation of the same output was presented in the Result Details, Output section.

JSON events are also in the Result Details widget, Events List.

Attachments

    Outcomes