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
2
  • 2
    JsonPointers are quite limited. For this kind of query you might want to look at JsonPath. Commented Sep 15, 2017 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? Commented Sep 8, 2020 at 11:31

3 Answers 3

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.
Sign up to request clarification or add additional context in comments.

Comments

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;
    }
  }

Comments

0

JSON Pointer is weak for this very reason. JSON Path is much better for this use case.

JSON PATCH unfortunately leverages JSON Pointer, and that too must be updated to be able to use JSON PATH instead. I am sure the authors will have heartburn. But to preprocess the data and then determine the index value to construct the JSON Pointer statement renders it obsolete.

1 Comment

Supporting information would improve the quality of this answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.