Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select an array member by name with a JSON Pointer

Tags:

jsonpointer

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
like image 652
jabbermonkey Avatar asked Dec 15 '16 18:12

jabbermonkey


1 Answers

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;
    }
  }
like image 116
jabbermonkey Avatar answered Sep 30 '22 20:09

jabbermonkey