Generic Object Index: .["foo"]
In the previous lesson we talked about
$.foo.bar
>.You can also do the same with $.["foo"]
or $.foo["bar"]
or $["foo"]["bar"]
$.foo
works only for identifier-like strings, if your key has any special characters like trunk-space
, you have to use $.["trunk-space"]
).
Now let's see if you can get postal code from the example JSON.
Input:
1{
2 "firstName": "Charles",
3 "lastName": "Doe",
4 "location": {
5 "city": "San Francisco",
6 "postal-code": "94103"
7 }
8}
JsonPath expression: Expected:
1[
2 "94103"
3]