从 JSON 中提取 ID 按值过滤
extract ID from JSON filtering by value
我有以下 JSON 文件
{
"businessEventData": [
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:03.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:15.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Profile",
"primaryKey": "45902",
"createdDateTime": "2021-09-25 11:58:03.0"
}
}
}
]
}
我需要提取 json 对象中的 primaryKey
,其中 moduleName 等于“Reservation”。我想要的输出是:
[
"45901",
"45901"
]
如果我 运行 followig json路径 $..header[?@.moduleName == "Reservation"].primaryKey
我没有得到任何输出。
另外,有没有办法获得具有 jsonPath 的唯一列表?而不是:
[
"45901",
"45901"
]
得到
[
"45901"
]
I need to extract the primaryKey in the json object where the moduleName is equal to "Reservation".
不是 jsonpath,而是 python 1 行
data = {
"businessEventData": [
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:03.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:15.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Profile",
"primaryKey": "45902",
"createdDateTime": "2021-09-25 11:58:03.0"
}
}
}
]
}
values = [x['businessEvent']['header']['primaryKey'] for x in data['businessEventData'] if
x['businessEvent']['header']['moduleName'] == 'Reservation']
print(values)
输出
['45901', '45901']
怎么样:
$.businessEventData[?(@.businessEvent.header.moduleName=='Reservation')]..header.primaryKey
我有以下 JSON 文件
{
"businessEventData": [
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:03.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:15.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Profile",
"primaryKey": "45902",
"createdDateTime": "2021-09-25 11:58:03.0"
}
}
}
]
}
我需要提取 json 对象中的 primaryKey
,其中 moduleName 等于“Reservation”。我想要的输出是:
[
"45901",
"45901"
]
如果我 运行 followig json路径 $..header[?@.moduleName == "Reservation"].primaryKey
我没有得到任何输出。
另外,有没有办法获得具有 jsonPath 的唯一列表?而不是:
[
"45901",
"45901"
]
得到
[
"45901"
]
I need to extract the primaryKey in the json object where the moduleName is equal to "Reservation".
不是 jsonpath,而是 python 1 行
data = {
"businessEventData": [
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:03.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Reservation",
"primaryKey": "45901",
"createdDateTime": "2021-09-26 11:58:15.0"
}
}
},
{
"businessEvent": {
"header": {
"moduleName": "Profile",
"primaryKey": "45902",
"createdDateTime": "2021-09-25 11:58:03.0"
}
}
}
]
}
values = [x['businessEvent']['header']['primaryKey'] for x in data['businessEventData'] if
x['businessEvent']['header']['moduleName'] == 'Reservation']
print(values)
输出
['45901', '45901']
怎么样:
$.businessEventData[?(@.businessEvent.header.moduleName=='Reservation')]..header.primaryKey