如何使用 JSON 路径从 JSON 数组中将不存在的道具提取为 null 或空字符串
How to extract non-existing props as null or empty String from a JSON array by using JSON path
下面我有Json这样的
{
'orderList': [
{
'productId': 'AAA',
},
{
'productId': 'BBB',
}
]
}
当 orderList
中的所有项都具有非空 productId
属性时,JSON 字符串有效,我正在尝试使用 JsonPath 来验证,现在我有个问题。 $.orderList[*]['productId']
可以从 orderList
中提取所有 productIds 作为字符串数组,如 ['AAA', 'BBB']
,这让我可以检查它的项目是否都是非空的,但是当 productId
prop 不存在时如下图
{
'orderList': [
{
'productId': 'AAA',
},
{
}
]
}
JSON路径
$.orderList[*]['productId']
获取一个数组作为 ['AAA']
(不是 ['AAA', null]
或 ['AAA', '']
),因此它可以通过我的检查功能。如何正确执行此操作?
这就是 jsonpath 的工作原理。如果它没有在对象中找到 属性,它会忽略结果数组中的那个,而不是添加 null
或 undefined
值。所以,如果你想在结果数组中得到 null
那么你需要添加 属性 和 null
作为值。
{
"orderList": [
{
"productId": "AAA"
},
{
"productId": null
}
]
}
将导致
[
"AAA",
null
]
下面我有Json这样的
{
'orderList': [
{
'productId': 'AAA',
},
{
'productId': 'BBB',
}
]
}
当 orderList
中的所有项都具有非空 productId
属性时,JSON 字符串有效,我正在尝试使用 JsonPath 来验证,现在我有个问题。 $.orderList[*]['productId']
可以从 orderList
中提取所有 productIds 作为字符串数组,如 ['AAA', 'BBB']
,这让我可以检查它的项目是否都是非空的,但是当 productId
prop 不存在时如下图
{
'orderList': [
{
'productId': 'AAA',
},
{
}
]
}
JSON路径
$.orderList[*]['productId']
获取一个数组作为 ['AAA']
(不是 ['AAA', null]
或 ['AAA', '']
),因此它可以通过我的检查功能。如何正确执行此操作?
这就是 jsonpath 的工作原理。如果它没有在对象中找到 属性,它会忽略结果数组中的那个,而不是添加 null
或 undefined
值。所以,如果你想在结果数组中得到 null
那么你需要添加 属性 和 null
作为值。
{
"orderList": [
{
"productId": "AAA"
},
{
"productId": null
}
]
}
将导致
[
"AAA",
null
]