Similar to XPATH for XML processing, JSON Pointer is used to reference a specific value within a JSON document. JSON Pointer is intended to be easily expressed in JSON string values as well as URI RFC3986 fragment identifiers:
Syntax type |
Syntax |
String |
Unicode string containing a sequence of zero or more reference tokens, each prefixed by a '/' (%x2F) character. Because the characters, '~' (%x7E) and '/' (%x2F), have special meanings in JSON Pointer, '~' needs to be encoded as '~0' and '/' needs to be encoded as '~1' when these characters appear in a reference token. |
URI |
A JSON Pointer can be represented in a URI fragment identifier by encoding it into octets using UTF-8 RFC3629, while percent-encoding the characters not allowed by the fragment rule in RFC3986. |
For example, let's consider the following JSON document:
{
"departmentId":10,
"departmentName":"IT",
"country":"US",
"manager":{"firstName":"John","lastName":"Chen","email":"[email protected]","doj":"2012-04-23T18:25:43.511Z"},
"directReportIds":["12345","67890"]
}
Let's examine how the values in the preceding JSON document can be referenced using JSON Pointer:
JSON pointer (string syntax) |
JSON pointer (URI syntax) |
Referenced value |
"" |
# |
The whole document |
"/departmentId" |
#/departmentId |
10 |
"/manager" |
#/manager |
{firstName:John,lastName:Chen, email:[email protected],doj:2012-04-23T18:25:43.511Z} |
"/manager/firstName" |
#/manager/firstName |
John |
"/directReportIds/0" |
#/directReportIds/0 |
12345 |