Search code examples
restxpathoauthjsonpathinsomnia

Is it possible to use JSONPath or XPath to get a query string?


I'm currently using Insomnia App to create some requests and one of my API responses I get a JSON with "redirect_uri" field and the value is actually a URL.

I need to get only the code parameter, so the JSONPath or XPath needs to return 8748d39f-1d4f-311f-92c6-4b279db1b317 in the following example:

{
  "redirect_uri": "http://www.google.com?state=string&code=8748d39f-1d4f-311f-92c6-4b279db1b317"
}

Value to return: 8748d39f-1d4f-311f-92c6-4b279db1b317

Is it possible with JSONPath or XPath?


Solution

  • You can do it with this somewhat convoluted xpath expression:

    substring-before(substring-after('{
      "redirect_uri": "http://www.google.com?state=string&code=8748d39f-1d4f-311f-92c6-4b279db1b317"
    }',
    'code='),'"')