如何编写JSONPath表达式筛选指定条件的文件子节点?
Solution for Targeting Valid .ord Files in JSONPath
Got it, let's refine your JSONPath expression to account for both file/directory distinctions and the specific extension you need. Here's the exact expression you should use:
$.children[?(@.isfile == true && @.extension == 'ord')].name
Breakdown of the Conditions:
@.isfile == true: This filters out any directory nodes (whereisfileisfalse) and ensures we only look at actual file entries.&& @.extension == 'ord': This adds the original check for files with the.ordextension, combining the two requirements with a logical AND.
Extra Rigor: Ensuring the extension Property Exists
If you want to be extra safe (in case some file nodes don't have an extension property at all), you can add an exists() check to avoid unexpected matches:
$.children[?(@.isfile == true && exists(@.extension) && @.extension == 'ord')].name
This version will skip any file nodes that don't have an extension field entirely, even if isfile is true.
Example Test Case
Let's say you have this JSON structure:
{ "children": [ { "name": "report.ord", "isfile": true, "extension": "ord" }, { "name": "data.csv", "isfile": true, "extension": "csv" }, { "name": "docs", "isfile": false }, { "name": "temp-file", "isfile": true } ] }
Using either expression above will return only ["report.ord"]—exactly the result you want, ignoring directories, non-.ord files, and files missing the extension property.
内容的提问来源于stack exchange,提问作者r3plica
相关产品推荐
相关产品推荐

