📜  json过滤器python(1)

📅  最后修改于: 2023-12-03 15:17:05.054000             🧑  作者: Mango

JSON过滤器 Python

JSON过滤器是指能够将JSON数据根据用户要求进行筛选、过滤或转换的工具。Python作为一门流行的编程语言,自然也有很多JSON过滤器的库和工具可供使用。这里将简单介绍几个常用的Python JSON过滤器。

jsonpath-rw库

jsonpath-rw是Python中一个非常流行的JSON路径解析库,它支持绝大部分 JSONPath语法,可以轻松地从JSON数据中提取数据或者对数据进行操作。

使用jsonpath-rw例子:

from jsonpath_rw import jsonpath, parse

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},
                           {"category": "fiction",
                            "author": "Herman Melville",
                            "title": "Moby Dick",
                            "isbn": "0-553-21311-3",
                            "price": 8.99},
                           {"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}}}

expression = parse("$.store.book[0].title")

result = [match.value for match in expression.find(data)]

print(result) # output: ['Sayings of the Century']

这段代码中,我们使用jsonpath-rw从JSON数据中找到 store.book[0].title 字段,输出结果为 ['Sayings of the Century']

JMESPath库

JMESPath是另一个功能强大的JSON路径解析库,与jsonpath-rw相比,JMESPath支持更多的特性和操作,例如通过过滤器以及函数等方式对数据进行筛选和转换。

使用JMESPath例子:

import jmespath

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},
                           {"category": "fiction",
                            "author": "Herman Melville",
                            "title": "Moby Dick",
                            "isbn": "0-553-21311-3",
                            "price": 8.99},
                           {"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}}}

expression = jmespath.compile('store.book[*].title')

result = expression.search(data)

print(result) # output: ['Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings']

这段代码中,我们使用JMESPath从JSON数据中找到所有 store.book[*].title 字段,输出结果为 ['Sayings of the Century', 'Sword of Honour', 'Moby Dick', 'The Lord of the Rings']

PyJMES库

PyJMES是一个基于JMESPath的Python库,可以方便地在Python中进行JSON过滤。除了JMESPath原有特性外,PyJMES还提供了一些增强特性,如支持指定结果的数据类型、支持动态变量等。

使用PyJMES例子:

from pyjmes import JMESPath

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},
                           {"category": "fiction",
                            "author": "Herman Melville",
                            "title": "Moby Dick",
                            "isbn": "0-553-21311-3",
                            "price": 8.99},
                           {"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}}}

expression = JMESPath('store.book[*].{title: title, price: price}')

result = expression.search(data)

print(result) # output: [{'price': 8.95, 'title': 'Sayings of the Century'}, {'price': 12.99, 'title': 'Sword of Honour'}, {'price': 8.99, 'title': 'Moby Dick'}, {'price': 22.99, 'title': 'The Lord of the Rings'}]

这段代码中,我们使用了PyJMES从JSON数据中找到 store.book[*].{title: title, price: price} 字段,输出结果为 [{'price': 8.95, 'title': 'Sayings of the Century'}, {'price': 12.99, 'title': 'Sword of Honour'}, {'price': 8.99, 'title': 'Moby Dick'}, {'price': 22.99, 'title': 'The Lord of the Rings'}]

总结

以上是Python中常用的几个JSON过滤器的介绍,这些过滤器都具有丰富的特性和功能,可以满足各种不同的JSON数据处理需求。如果你在处理JSON数据时需要筛选、过滤或转换数据,这些工具是值得推荐的选择。