# JSON Path Syntax
JSON Path is used to locate the data in a JSON object. JSON Path is currently used in the following two functionalities of vREST:
- Variable Extractor tab In the variable extractor tab of a test case, you need to specify the JSON path in the "Path" column.
- JSON Body Assertions In the Assertions tab, you need to specify JSON Path for JSON Body assertions in the "Property" column.
Note: We are using jsonPath version 0.8.5. Just remove the prefixes "$." in the case of the JSON object and "$" in the case of the top-level JSON array while defining JSON path expressions in vREST.
Below are some examples of the most common scenarios for JSON path expressions.
# For JSON Object Data
Below is the sample JSON Object for reference.
{
"summary": "Sample Test Case ...",
"method": "GET",
"url": "http://localhost:3000/sample-test-case",
"expectedResults": {
"statusCode": 200,
"execution time": 453,
"headers": [
{
"value": "Mon, 27 Jul 2015 06:38:31 GMT",
"name": "Date"
},
{
"value": "application/json",
"name": "Content-Type"
}
]
},
"createdAt": "2015-02-09T08:22:18.000Z",
"version.0": "Version 0",
"version.1": "Version 1"
}
- Top Level Property
- JSON Path Expression: method
- Value: GET
- Nested Property
- JSON Path Expression: expectedResults.statusCode
- Value: 200
- Nested Property inside an array
- JSON Path Expression: expectedResults.headers.0.name
- Value: Date
- To verify the headers array length
- JSON Path Expression: expectedResults.headers.length
- Value: 2
- When key also having a dot (Use Square Brackets)
- JSON Path Expression
['version.0']
- Value: Version 1
- JSON Path Expression
- When key having spaces (Use Square Brackets)
- JSON Path Expression
expectedResults['execution time']
- Value: 453
- JSON Path Expression
- To extract the full response in a variable
- JSON Path Expression
$
- Value:
Full JSON as an object
- JSON Path Expression
# For JSON Array Data
Below is the sample JSON Array for reference.
[
{
"summary": "Sample Test Case 1",
"method": "GET",
"url": "http://localhost:3000/sample-test-case1"
},
{
"summary": "Sample Test Case 2",
"method": "GET",
"url": "http://localhost:3000/sample-test-case2"
},
{
"summary": "Sample Test Case 3",
"method": "GET",
"url": "http://localhost:3000/sample-test-case3"
}
]
- Nested property from Top level Array
- JSON Path Expression:
[0].method
- Value: GET
- JSON Path Expression:
- To verify the length of Top level Array
- JSON Path Expression: length
- Value: 3