In 4D v16 R4, we enriched object commands so you can validate JSON objects. 4D v16 R5 is going even further by introducing the support of JSON pointers. Great – but what is a JSON pointer and what can I do with it?
Just like you don’t store company details for each employee in a database record (using a link to the company record instead), it can be really useful to structure a JSON document with parts that can be reused in a number of places.
For instance, let’s take an example of a website order; the customer enters the shipping address and designates the billing address as the same. Instead of duplicating the address information, we can define the shipping address as a reference to the the billing address.
Another scenario is to define a template object containing default properties stored in JSON format and merges some of the properties with information stored in a separate JSON file.
Objects that contain cycles (i.e. two objects that contain each other) cannot be stringified in JSON. JSON pointers are a way to express the cyclic reference while maintaining compatibility with the JSON format.
JSON Pointer CONCEPT
JSON Pointer is a standard which defines a string syntax that can be used to access a particular field or key value in the same or another JSON document. It’s made up of strings separated by the “/” character. These strings either specify keys in objects or indexes in arrays. The JSON pointer syntax is defined by RFC 6901.
For example, this object:
{ "foo": {"$ref": "#/bar"}, "bar": "42" }
Can be resolved as:
{ "foo": "42", "bar": "42" }
NEW COMMAND
The JSON resolve pointers command analyzes all the JSON pointers found in the object passed as parameter with regards to some options settings (if any). The command then returns an object with the success status, the result value and the potential list of errors.
A FEW Examples… it’s always helpful!
Check out our example database demo to learn (and understand) more about how JSON pointers work!
In addition to the demo, here’s another example – let’s take an $object variable which contains the following:
If I run the code below:
$result:=JSON Resolve pointers($object)
TRACE
Here’s what I get in the $object and $result variables:
Note that $result is an object, containing both the success status and the returned value. If there had been any errors, they’d have been included, too.