Create a JSONpath expression so that it retrieves only a specific value?

I have some JSON, of which the following is a small number:

{
    "results": {
        "div": [
            {
                "class": "sylEntry",
                "div": [
                    {
                        "class": "sT",
                        "id": "sOT",
                        "p": "Mon 11/17, Computer work time"
                    },
                    {
                        "class": "des",
                        "id": "dOne",
                        "p": "All classes Siebel 0218"
                    }
                ],
                "id": "sylOne"
            }
        ]
    }
}

I would like to get only the content "p"for the div element with the class "sT". I would like to use a loop and do something like this:

var arrayOfResults = $.results..div.p 

does not work because I only want to get the p value for the div element with the class "sT".

So, how do I build my JSON path so that it retrieves an array of p elements that are contained in the ss divs class.

Thank!!

+3
source share
3 answers

Concept

JSONPath, -, , Javascript . @ node. :

$.. book [? (@. price < 10)]// , 10

, , , , .

, , jsonpath-test-js.html JSONPath repo, . - HTML .

o p. o , p JSONPath o. p o s, . , REPL, .

:

<html>
<head>
<title> JSONPath - Tests (js)</title>
<script type="text/javascript" src="http://www.json.org/json.js"></script>
<script type="text/javascript"
        src="http://jsonpath.googlecode.com/svn/trunk/src/js/jsonpath.js">
</script>
</head>
<body>
<pre>
<script type="text/javascript">
var out = "", tests = 
[ { "o": { "results" : {  "div" : [  {  "clazz": "sylEntry",
           "id": "sylOne",  "div": [  {  "clazz": "sT",  "id": "sOT",
           "p": "Mon 11/17, Computer work time"  },  {  "clazz": "des",
           "id": "dOne",  "p": "All classes Siebel 0218"  }  ]  }  ]  }  },
    "p": ["$.results..div[?(@.clazz=='sT')].p", // my suggestion expression
          "$.results..div[*].p"]},             // your question expression
];

function evaluate($, p) {
  var res = eval(p);
  return res != null ? res.toJSONString() : null;
}

for (var i=0; i<tests.length; i++) {
   var pathes;
   for (var j=0; j<tests[i].p.length; j++) {
      pre = ">";
      if (pathes = jsonPath(tests[i].o, tests[i].p[j], {resultType: "PATH"}))
         for (var k=0; k<pathes.length; k++) {
            out += pre + " " + pathes[k] +
                   " = " + evaluate(tests[i].o, pathes[k]) + "\n";
            pre = " ";
         }
      }
      out += "<hr/>";
   }
   document.write(out);
  </script>
</pre>
</body>
</html>

, , , .

, :

> $['results']['div'][0]['div'][0]['p'] = "Mon 11/17, Computer work time"
> $['results']['div'][0]['div'][0]['p'] = "Mon 11/17, Computer work time"
  $['results']['div'][0]['div'][4]['p'] = "All classes Siebel 0218"

, ==, :

$.results..div[?(@.class=='sT')].p

( , Javascript- JSONPath): "" :

SyntaxError: jsonPath: Parse error: _v.class=='sT'

, eval -, JSONPath. class Javascript, . @.class:

$.results..div[?(@.['class']=='sT')].p

:

> $['results']['div'][0]['div'][0]['p'] = "Mon 11/17, Computer work time"
> $['results']['div'][0]['div'][0]['p'] = "Mon 11/17, Computer work time"
  $['results']['div'][0]['div'][5]['p'] = "All classes Siebel 0218"

, , ! , , , !

+10

DefiantJS (http://defiantjs.com), JSON "" - JSON XPath. ( , ).

JSfiddle ;
http://jsfiddle.net/hbi99/sy2bb/

var data = {
       "results": {
          "div": {
             "class": "sylEntry",
             "id": "sylOne",
             "div": [
                {
                   "class": "sT",
                   "id": "sOT",
                   "p": "Mon 11/17, Computer work time"
                },
                {
                   "class": "des",
                   "id": "dOne",
                   "p": "All classes Siebel 0218"
                }
             ]
          }
       }
    },
    res = JSON.search( data, '//div[class="sT"]/p' );

console.log( res[0] );
// Mon 11/17, Computer work time

XPath , XPath Evaluator:
http://defiantjs.com/#xpath_evaluator

+1

JsonPath.with(jsonResponse).param( "name", "getName" ). get ( "findAll {a → a.name == name}" )

0

Source: https://habr.com/ru/post/1747261/


All Articles