我们如何在 jsonpath_ng python 中使用正则表达式过滤器,它将 / 视为 SORT 方向。还有其他选择吗?

How do we use regex filter in jsonpath_ng python, it treats / as SORT Direction. any alternative?

我正在尝试查找其 Worker 值包含数字 880099 作为子字符串的 Worker。我在这里尝试使用 priyanka 的员工 ID 查找她的详细信息。

我的代码。

json_data = json.loads(json_string)
from jsonpath_ng.ext import parser
query = r'$[?(@.Worker =~ /.*?880099.*/i)]'
for match in parser.parse(query).find(json_data):
   
   

JSON 数据: [{ “休假条目”:“2021 年 2 月 1 日 - 1 天(Divakar (77877))”, “工人”:“Divakar (77877)”, “请求类型”:“休假请求”, "Time Off/Absence Table": "IND 年度休假", "类型": "年假", “输入时间”:1611187200000, “批准日期”:1611187200000, “休假日期”:1612137600000, “批准”:1, “时间单位”:“天” }, { “休假条目”:“2021 年 2 月 1 日 - 1 天 (Priyanka (880099))”, “工人”:“普里扬克斯(880099)”, “请求类型”:“休假请求”, "时间 Off/Absence Table": "IND Sick/Casual 休假", "类型": "Sick/Casual离开", “输入时间”:1612310400000, 《批准日期》:1612310400000, “休假日期”:1612137600000, “批准”:1, “时间单位”:“天” }]

您用于正则表达式比较的 jsonpath-ng 语法不正确。尝试在模式周围加上引号,而不是像这样:

parser.parse('$[?(@.Worker =~ ".*880099.*")]').find(json_data)]

完整样本:

import json
json_string =  "[{ \"Time Off Entry\": \"1 Feb, 2021 - 1 Days (Divakar (77877))\", \"Worker\": \"Divakar (77877)\", \"Request Type\": \"Time Off Request\", \"Time Off/Absence Table\": \"IND Annual Time Off\", \"Type\": \"Annual Leave\", \"Entered On\": 1611187200000, \"Approval Date\": 1611187200000, \"Time Off Date\": 1612137600000, \"Approved\": 1, \"Unit of Time\": \"Days\" }, { \"Time Off Entry\": \"1 Feb, 2021 - 1 Days (Priyanka (880099))\", \"Worker\": \"Priyanks (880099)\", \"Request Type\": \"Time Off Request\", \"Time Off/Absence Table\": \"IND Sick/Casual Time Off\", \"Type\": \"Sick/Casual Leave\", \"Entered On\": 1612310400000, \"Approval Date\": 1612310400000, \"Time Off Date\": 1612137600000, \"Approved\": 1, \"Unit of Time\": \"Days\" }]"
json_data = json.loads(json_string)

from jsonpath_ng.ext import parser
query = [x.value for x in parser.parse('$[?(@.Worker =~ ".*880099.*")]').find(json_data)]
print(json.dumps(query))