Select an array member by name with a JSON Pointer

13.1k Views Asked by At

Is there a way to select an array member the value of a key with JSON Pointer? So for this JSON Schema:

"links":[
    {
      "title": "Create",
      "href": "/book",
      "method": "POST",
      "schema": {}
    },
    {
      "title": "Get",
      "href": "/book",
      "method": "GET",
      "schema": {}
    }
  ]

Instead of:

links/0/schema

I would like to be able to do:

links/{title=GET}/schema
2

There are 2 best solutions below

0
On

Apparently not. So I did this:

const links = schema.links;
  let ref;
  for (const [i, link] of links.entries()) {
    if (link.href === req.originalUrl && link.method === req.method) {
      ref = `schema.json#/links/${i}/schema`;
      break;
    }
  }
0
On

The Json Pointer defined in RFC6901 doesn't allow you to select an array member by name. Here is the only mention of Arrays in the RFC:

If the currently referenced value is a JSON array, the reference token MUST contain either:

* characters comprised of digits ..., or

* exactly the single character "-", making the new referenced
         value the (nonexistent) member after the last array element.