JsonPath用法详解

JSONPath是一种信息抽取类库,是从JSON文档中抽取指定信息的工具,提供多种语言实现版本,包括Javascript、Python、PHP和Java。

JSONPath的安装方法如下:

pip install jsonpath

JSONPath语法和XPATH语法对比 JSON结构清晰,可读性高,复杂度低,非常容易匹配。JSONPath的语法与Xpath类似,如下表所示为JSONPath与XPath语法对比。
在这里插入图片描述下面使用一个JSON文档演示JSONPath的具体使用。JSON 文档的内容如下:

{
  "store": {
    "book":[
      { "category": "reference",
        "author": "Nigel Rees",
        "title": "Sayings of the Century",
        "price": 8.95
      },
      { "category": "fiction",
        "author": "J. R. R. Tolkien",
        "title": "The Lord of the Rings",
        "isbn": "0-395-19395-8",
        "price": 22.99
      }
    ],
    "bicycle": {
      "color": "red",
      "price": 19.95
    }
  }
}

假设变量bookJson中已经包含了这段JSON字符串,可通过以下代码反序列化得到JSON对象:

books=json.loads(bookJson)

(1)查看store下的bicycle的color属性:

checkurl = "$.store.bicycel.color"
print(jsonpath.jsonpath(books, checkurl))
# 输出:['red']

(2)输出book节点中包含的所有对象:

checkurl = "$.store.book[*]"
object_list=jsonpath.jsonpath(books, checkurl)
print(object_list)

(3)输出book节点的第一个对象:

checkurl = "$.store.book[0]"
obj = jsonpath.jsonpath(books, checkurl)
print(obj)
# 输出: ['category': 'reference', 'author': 'Nigel Rees', 'title': 'Sayings of the Century', 'price': 8.95}]

(4)输出book节点中所有对象对应的属性title值:

checkurl = "$.store.book[*].title"
titles = jsonpath.jsonpath(books, checkurl)
print(titles)
# 输出: ['Sayings of the Century', 'The Lord of the Rings']

(5)输出book节点中category为fiction的所有对象:

checkurl = "$.store.book[?(@.category=='fiction')]”
books=jsonpath.jsonpath(books, checkurl)
print(books)
# 输出:[{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lordof the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]

(6)输出book节点中所有价格小于10的对象:

checkurl="$.store.book[?(@.price<10)]"
books = jsonpath.jsonpath(books, checkurl)
print(books)
# 输出: [{'category': 'reference', 'author': 'Nigel Rees', 'title':'Sayings of the Century', 'price': 8.95}]

(7)输出book节点中所有含有isb的对象:

checkurl = "$.store.book[?(@.isb)]"
books = jsonpath.jsonpath(books,checkurl)
print(books)
# 输出: [{'category': 'fiction', 'author': 'J. R. R. Tolkien', 'title': 'The Lord of the Rings', 'isbn': '0-395-19395-8', 'price': 22.99}]
JSONPath 是一种用于在 JSON 数据中定位和提取特定数据的查询语言。它类似于 XPath 对于 XML 数据的作用。以下是 JSONPath 的一些常用语法: 1. `$`:表示根节点。 2. `.`:表示当前节点。 3. `..`:表示递归下降,用于查找某个节点下的所有子孙节点。 4. `*`:表示通配符,匹配任意节点。 5. `@`:表示当前节点的属性值。 6. `[]`:用于过滤数组中的元素。 - `[0]`:表示获取数组中的第一个元素。 - `[1,2]`:表示获取数组中的第二个和第三个元素。 - `[start:end]`:表示获取数组中从 start 索引到 end 索引范围内的元素。 - `[?(expression)]`:表示根据表达式过滤数组中的元素。 7. `.` 或 `[]` 后跟属性名或索引值,用于访问对象属性或数组元素。 例如,给定以下 JSON 数据: ``` { "store": { "book": [ { "category": "fiction", "title": "Book 1" }, { "category": "fiction", "title": "Book 2" } ], "bicycle": { "color": "red", "price": 19.95 } } } ``` 可以使用以下 JSONPath 表达式来获取特定的数据: - `$.store.book[0].title`:获取第一本书的标题。 - `$.store.book[*].title`:获取所有书的标题。 - `$.store..title`:获取所有节点名为 "title" 的值。 - `$.store.book[?(@.category=='fiction')]`:根据条件获取所有类别为 "fiction" 的书。 这只是 JSONPath 的一小部分语法,你可以根据具体需求使用更复杂的表达式来查询和提取 JSON 数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值