25

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
jabbermonkey
  • 1,680
  • 4
  • 19
  • 37
  • 1
    JsonPointers are quite limited. For this kind of query you might want to look at JsonPath. – dnault Sep 15 '17 at 22:24
  • 5
    I am extremely appalled this question has so little exposure and, as it seems right now, does not have a proper implementation as a resolution for this request. How is it by default expected that each array member has always the same index? – Akito Sep 08 '20 at 11:31

2 Answers2

8

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.
Community
  • 1
  • 1
3

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;
    }
  }
jabbermonkey
  • 1,680
  • 4
  • 19
  • 37