JSONPath and Traverson filtering: single element array

Here is the Json I am trying to handle with Traverson to extract a single link from this HAL:

{
  "_links": {
    "curies": [
      {
        "href": "https://localhost/auth/def/rels/{rel}",
        "name": "auth",
        "templated": true
      }
    ],
    "self": {
      "href": "https://localhost/auth/identity"
    }
  },
  "_embedded": {
    "auth": [
      {
        "_links": {
          "auth:ad": [
            {
              "href": "https://localhost/auth/ad"
            }
          ]
        },
        "kind": "oauth2"
      },
      {
        "_links": {
          "auth:default": [
            {
              "href": "https://localhost/auth/default" // **Link that I need**
            }
          ]
        },
        "kind": "oauth2"
      }
    ]
  }
}

Java code responsible for handling ideally should look like this:

Link test = traverson
        .follow("auth")
        .follow("$.['_embedded']['auth']..['_links']['auth:default']..['href'][0]")
        .asLink();

But for some reason I am getting no hits despite the fact that right before adding the last [0] in the JSONPath expression I am getting an array of one element. Is there a way to extract this single link so that I am not returned with an array? I used this tool for testing.

1
Leave a Reply

avatar
1 Comment threads
0 Thread replies
0 Followers
 
Most reacted comment
Hottest comment thread
1 Comment authors
Jason Recent comment authors
  Subscribe  
newest oldest most voted
Notify of
Jason
Guest

The code necessary to achieve what I should look like this:

JsonPathLinkDiscoverer disc = new JsonPathLinkDiscoverer("$._embedded..auth.._links..['%s']..href", MediaTypes.HAL_JSON);
Link authLink = disc.findLinksWithRel("auth:default", <json>).get(0);

where <json> should be the json representation of the example in the original question (probably like a reponse from other call).