Python Liquid的JSONPath选择器。
项目描述
Liquid JSONPath
JSONPath选择器用于Python Liquid。
目录
安装
使用pip安装JSONPath for Liquid
python -m pip install -U liquid-jsonpath
pipenv install liquid-jsonpath
链接
- 文档:https://jg-rp.github.io/liquid/jsonpath/introduction
- 变更日志:https://github.com/jg-rp/liquid-jsonpath/blob/main/CHANGES.md
- PyPi:https://pypi.ac.cn/project/liquid-jsonpath/
- 问题跟踪:https://github.com/jg-rp/liquid-jsonpath/issues
示例
过滤器
此示例将find
过滤器添加到Liquid环境中。您可以将find
视为标准map
和where
过滤器的先进替代品。它接受一个JSONPath字符串参数,并将其应用于过滤器的左侧值。
from liquid import Environment
from liquid_jsonpath import Find
env = Environment()
env.add_filter("find", Find())
data = {
"users": [
{
"name": "Sue",
"score": 100,
},
{
"name": "John",
"score": 86,
},
{
"name": "Sally",
"score": 84,
},
{
"name": "Jane",
"score": 55,
},
]
}
template = env.from_string("{{ data | find: '$.users.*.name' | join: ' ' }}")
print(template.render(data=data)) # Sue John Sally Jane
标签
此示例用支持通过JSONPath表达式管道化可迭代对象的标签替换了标准的{% for %}
标签。
from liquid import Environment
from liquid_jsonpath import JSONPathForTag
env = Environment()
env.add_tag(JSONPathForTag)
data = {
"users": [
{
"name": "Sue",
"score": 100,
},
{
"name": "John",
"score": 86,
},
{
"name": "Sally",
"score": 84,
},
{
"name": "Jane",
"score": 55,
},
]
}
template = env.from_string(
"{% for name in data | '$.users.*.name' %}"
"{{ name }}, "
"{% endfor %}"
)
print(template.render(data=data)) # Sue, John, Sally, Jane,
许可协议
liquid-jsonpath
根据MIT许可协议分发。