Search code examples
jsonxpathjsonpath

Extract values from JSON array using XPath?


I'm pretty new to XPath... I need to extract only the values 2124,143 from this JSON array using XPath.

I tried few ways but didn't succeed. Any idea?

{  
       "legend_size":1,
       "data":{  
          "series":[  
             "2016-06-01",
             "2016-07-01"
          ],
          "values":{  
             "Download":{  
                "2016-07-01":143,
                "2016-06-01":2124
             }
          }
       }
    }

Solution

  • XPath (traditionally, prior to v3.1, which is not widely supported) is defined over XML, not JSON.

    Alternatively, consider JSONPath, an "XPath for JSON," which is also supported by Dojo Toolkit.

    In JSONPath, 2124 could be accessed as $.data.values.Download.2016-06-01