Array Index: .[2]
Just like in most programming languages you can get element from array using operator []
If top level field is an array you can do $.[1]
, if array is nested somewhere then you need to use nested fields first $.car.options[1]
Arrays are zero-based, so $.[2]
returns the third element.
Negative indices are allowed, with -1
referring to the last element, -2
referring to the next to last element, and so on.
Now see if you can fetch first hobby of a person represented by a JSON below.
Input:
1{
2 "firstName": "Charles",
3 "lastName": "Doe",
4 "hobbies": [
5 "chess",
6 "netflix",
7 "fitness"
8 ]
9}
JsonPath expression: Expected:
1[
2 "chess"
3]