Select an array member by name with a JSON Pointer
Asked Answered
H

2

32

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
Hin answered 15/12, 2016 at 18:34 Comment(2)
JsonPointers are quite limited. For this kind of query you might want to look at JsonPath.Espinal
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?Twoply
G
9

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.
Gingras answered 21/9, 2020 at 12:40 Comment(0)
H
4

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;
    }
  }
Hin answered 22/12, 2016 at 19:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.