Pjj可以从json文档或dict、list中快速检索返回内容,支持模糊查询
PJJ provides a fast and simple way to retrieve values from JSON documents.
点连接字符串解析json 支持:
- 点连接查询
- 通配符 ['?', '*'] 模糊查询
- #符返回字典keys列表、列表长度
- 字典条件查询
- 双引号强制字符串查询(列表内对象key和索引冲突时,默认返回索引的对象,双引号后返回该key的列表)
from pjj import Pjj
test_json = '''
{
"top":"a",
"mid":{"m":1, "i":2, "d":3},
"tail":[11,22,33],
"test.abc":"success",
"mmm":[{"a1":"A", "b1":"B"}, {"a1":"C", "b1":"D"}],
"force_string":[{"1":"a1", "2":"a2"}, {"1":"b1", "2":"b2"}],
"filter": [
{"name":"judy", "age":24},
{"name":"tom", "age":30},
{"name":"jerry", "age":28},
{"name":"\\"<c>\\"", "age":24}
]
}
'''
# ------------basis-------------
print(Pjj('top', test_json).res)
# result: "a"
print(Pjj('mid', test_json).res)
# result: {"m":1, "i":2, "d":3}
print(Pjj('mid.m', test_json).res)
# result: 1
print(Pjj('#', test_json).res)
# result: ['top', 'mid', 'tail', 'test.abc', 'mmm', 'force_string', 'filter']
# ----------wildcard-----------
print(Pjj('t?p', test_json).res)
# result: "a"
print(Pjj('to*', test_json).res)
# result: "a"
print(Pjj('f*r', test_json).res)
# result: [{'name': 'judy', 'age': 24}, {'name': 'tom', 'age': 30}, {'name': 'jerry', 'age': 28}, {'name': '"<c>"', 'age': 24}]
# ---------index based----------
print(Pjj('tail.0', test_json).res)
# result: 11
# ----------list length---------
print(Pjj('mmm.#', test_json).res)
# result: 2
print(Pjj('mmm.#.a1', test_json).res)
# result: ["A", "C"]
print(Pjj('mmm.1.a1', test_json).res)
# result: "C"
# -----------wildcard-----------
print(Pjj('mmm.b?', test_json).res)
# result: ["B", "D"]
# ---------string escape-------
print(Pjj('test\.abc', test_json).res)
# result: "success"
# ---------force string--------
print(Pjj('"test.abc"', test_json).res)
# result: "success"
print(Pjj('force_string.1',test_json).res)
# result: {'1': 'b1', '2': 'b2'}
print(Pjj('force_string."1"',test_json).res)
# result: ['a1', 'b1']
# ---------list query-----------
print(Pjj('filter.#(name=="judy").age', test_json).res)
# result: "[24]"
print(Pjj('filter.#(age>26).name', test_json).res)
# result: ['tom', 'jerry']
print(Pjj('filter.#(age>26&&name=="tom")', test_json).res)
# result: [{'name': 'tom', 'age': 30}]
print(Pjj('filter.#(age>29||age<=25)', test_json).res)
# result: [{'name': 'judy', 'age': 24}, {'name': 'tom', 'age': 30}, {'name': '"<c>"', 'age': 24}]
print(Pjj('fil*.#(name!="judy").#(age>=30)', test_json).res)
# result: [{'name': 'tom', 'age': 30}]
print(Pjj('fil*.#(name=="\\"<c>\\"")', test_json).res)
# result: [{'name': '"<c>"', 'age': 24}]
空输入返回完整对象字典列表筛选- 数字列表筛选
- 可以对对象赋值
下划线搭配通配符匹配不了、多个通配符匹配不了如果对象带反斜杠会异常条件查询时如果匹配对象带比较运算符或者逻辑运算符会异常如果列表下的对象key为整数,只能返回索引对象不能返回列表key对应的value列表使用双引号强制字符串条件筛选:如果对象key为浮点数会异常使用双引号强制字符串