基本语法
JSONPath语法元素和对应XPath元素的对比
xpath索引下标是从1开始的
jsonpath索引下标是从0开始
Python中使用
安装依赖
pip install jsonpath
代码示例
# -*- coding: utf-8 -*- import jsonpath data = { "store": { "book": [ {"category": "reference", "author": "Nigel Rees", "title": "Sayings of the Century", "price": 8.95 }, {"category": "fiction", "author": "Evelyn Waugh", "title": "Sword of Honour", "price": 12.99 } ], "bicycle": { "color": "red", "price": 19.95 } } } ret = jsonpath.jsonpath(data, '$.store.book[*].author') print(ret) # ['Nigel Rees', 'Evelyn Waugh'] ret = jsonpath.jsonpath(data, '$..author') print(ret) # ['Nigel Rees', 'Evelyn Waugh'] ret = jsonpath.jsonpath(data, '$.store..price') print(ret) # [8.95, 12.99, 19.95] ret = jsonpath.jsonpath(data, '$..book[1].title') print(ret) # ['Sword of Honour'] ret = jsonpath.jsonpath(data, '$..book[?(@.price<10)].title') print(ret) # ['Sayings of the Century']
参考