11

Basically, I need to get a part of the string value using JSONPath. I cannot get the full value using JSONPath first and then get the part of it using another language.

Is there any way to do it in JSONPath?

Leon
  • 1,011
  • 1
  • 11
  • 28

1 Answers1

0

It would be great if you could exemplify some JSON data. Based on what you've written, I suspect, you're looking for something like this (here is the JSFilddle: http://jsfiddle.net/hbi99/4WYkc/);

var data = {
       "store": {
          "book": [
             {
                "@price": 12.99,
                "title": "Sword of Honour",
                "category": "fiction",
                "author": "Evelyn Waugh"
             },
             {
                "@price": 22.99,
                "title": "The Lord of the Rings",
                "category": "fiction",
                "author": "J. R. R. Tolkien",
                "isbn": "0-395-19395-8"
             }
          ],
          "bicycle": {
             "@price": 19.95,
             "brand": "Cannondale",
             "color": "red"
          }
       }
    },
    found = JSON.search(data, '//*[contains(title, "Lord")]');

document.getElementById('output').innerHTML = found[0].title;

JSONPath is not a standardised "query language" but XPath is. The JS library DefiantJS extends the global object JSON with the method "search", with which you can query JSON structure with XPath expressions. The method returns the matches as an array-like object.

To see more examples, check out the live XPath Evaluator here: http://defiantjs.com/#xpath_evaluator

Hakan Bilgin
  • 1,113
  • 12
  • 11
  • 3
    Thanks for that. However, your answer returns the whole title which is not what I want. I need a part of the title e.g. anything after "of" in the title. – Leon May 19 '14 at 10:33
  • 1
    I guess you're looking for a solution that doesn't result in post-parsing on your end. If you find such solution, share it here. – Hakan Bilgin May 19 '14 at 20:54