This topic provides information on selecting JSON elements in the JSON Assertor and JSON Data Bank tools in Virtualize.
Sections include:
For working with JSON and selecting nodes,Virtualize construct an XML DOM "under the hood" in order to use XPath for node selection. The XML structure very closely matches the JSON structure. All names and values are equivalent, except for the following cases:
The root node of the document is called root
in the XPath, but is an object or array reference in the JSON document. It appears as {}
or []
inVirtualize JSON trees.
Array items are called item
in the XPath, but are unnamed in the JSON document (by nature of being array elements). They appear as [0]
, [1]
, etc. inVirtualize JSON trees.
JSON names that contain characters that are illegal in XML element names are transformed to create legal XML element names for use in XPaths.Virtualize JSON trees will show the original names as they appear in the JSON document.
When you select a JSON tree node,Virtualize also create a corresponding XPath.
If you need to manually construct an XPath and you aren’t sure what node names to use in your XPath, we suggest that you use the JSON tree to select a node. This will cause an XPath to be generated that you can then review and modify to meet your specific needs.
The following options are available in the dialog that opens when you click Change Element (for JSON Assertor) or Modify (for JSON Data Bank).
[1]
can be changed to [2]
if you are interested in the 2nd occurrence only. After editing the XPath text, click the Evaluate XPath button to validate the XPath formatXPath/Parent
will output <parent>VALUE</parent>
. You can configure Index to extract. Default value is 1
. This controls which element is extracted in the case where the element occurs more than one time.XPath/Parent
will output VALUE
. You can configure Text Content, which extracts the text content of the element selected, or All Child Nodes, which extracts all child nodes of the element selected.${My Value}
in literal or multiple response views.Writable data source column: TellsVirtualize to store the value in a writable data source column. This allows you to store an array of values. Other tools can then iterate over the stored values.
Variable: Enables you to save the value in the specified variable so it can be reused across the current test, Responder, or Action suite. The variable must already be added to the current suite as described in Defining Variables in Virtualize. Any values set in this manner will override any local variable values specified in the suite properties panel.
For headers, you can configure the header name, as well as the data source column options listed above.
You can parameterize XPaths to reference test or Responder suite variables, environment variables, and data source values. The syntax to reference variables is ${myVariableName}
. The syntax to reference Data Bank values and Data Source Values is: ${myColumnName}
.
For example, if ${XPath Key}
is a data source column name, you could use the following:
/*[local-name(.)="bookstore" and namespace-uri(.)=""]/*[local-name(.)="book" and namespace-uri(.)=""][child::node()[local-name(.)="title" and text()="${XPath Key}"]]]