Search code examples
xpath

How to access a JSON object with XPath?


In a page, there is an inline element, containing multiple objects.

I can access the full script, using an XPath like //script[contains(text(),'results')].

It contains multiple objects, among others, this one

{
"results": [
      {"entry":[...]},
      {"entry":[...]}
   ]
}

How can I access only results with XPath?


Solution

  • I guess you're probably running client-side in the browser?

    Natively, the browsers only support XPath 1.0, which was defined long before JSON was invented, so you are out of luck there.

    There are a couple of third-party libraries that implement a more up-to-date version of XPath: one is my company's product SaxonJS which implements XPath 3.1. With XPath 3.1, given the JSON string shown in your question, you can do:

    let $json := ....,
        $entries := parse-json($json)?results
    return 
      for $i in 1 to array:size($entries)
      return $entries($i)
    

    But the detail depends on what you want to do with the JSON content...

    Alternatively, you might be able to find a suitable library that implements JSONPath, which is a language similar to XPath for accessing JSON rather than XML.