Skip to content

Latest commit

 

History

History
69 lines (56 loc) · 1.78 KB

0158-cl-json-pointer.org

File metadata and controls

69 lines (56 loc) · 1.78 KB

cl-json-pointer

This library implements RFC 6901 - a format for accessing nested JSON data-structures. It some sense, JSON pointer is similar to JSON path, but more suitable for use as a part of the URL fragment.

cl-json-pointer’s README provides many examples, but all of them are applied to the object which almost flat. Let’s try to reproduce an example from the JSON path’s site:

{
  "firstName": "John",
  "lastName" : "doe",
  "age"      : 26,
  "address"  : {
    "streetAddress": "naist street",
    "city"         : "Nara",
    "postalCode"   : "630-0192"
  },
  "phoneNumbers": [
    {
      "type"  : "iPhone",
      "number": "0123-4567-8888"
    },
    {
      "type"  : "home",
      "number": "0123-4567-8910"
    }
  ]
}

Now we’ll translate this JSON path: $.phoneNumbers[0].type into JSON pointer /phoneNumbers/0/type:

POFTHEDAY> (defparameter *obj* 
             (jsown:parse
              (alexandria:read-file-into-string "data.json")))

POFTHEDAY> (cl-json-pointer:get-by-json-pointer *obj* "/phoneNumbers/0/type"
                                                :flavor :jsown)
"iPhone"
("type" . "iPhone")
NIL

It is also possible to add/set/delete elements using cl-json-pointer.

You will find more examples in the official docs.

Comparing to the JSON path, the pointer has clearer character escaping rules and is able to work with keys containing dots and slashes and other symbols. But it does not support slicing and some other features of the JSON path.