[英]PHP How to count the number of items in JSON results but not the total number the number of specific results?
[英]Count the total items and items with specific condition in a json using Jolt
输入 JSON 为:
{
"subOrderItems": [
{
"travellerDetails": {
"title": "Ms.",
"name": "XYZ",
"gender": "F"
},
"random_key": "random_key",
"random_value": "random_value"
},
{
"travellerDetails": {
"title": "Mr.",
"nationality": "AU",
"name": "ABC",
"gender": "M"
},
"random_key": "random_key",
"random_value": "random_value"
},
{
"travellerDetails": {
"title": "Mr.",
"nationality": "UK",
"name": "PQR",
"gender": "M"
},
"random_key": "random_key",
"random_value": "random_value"
}
]
}
json 有“SubOrderItems”列表,它是 JSON 对象的列表。 首先,我想计算该列表中元素的数量。 其次,每个 json 都有一个名为“性别”的字段,因此希望统计男性人数和女性人数。
预计 Output:
{
"No.of People": 3,
"No.of Males": 2,
"No.of Females": 1
}
您可以通过按gender
属性分隔来创建列表,然后分别计算它们的大小,例如
[
{
"operation": "shift",
"spec": {
"subOrderItems": {
"*": {
"travellerDetails": {
"gender": {
"M": {
"@1": "&1[]",
"@": "P[]"
},
"F": {
"@1": "&1[]",
"@": "P[]"
}
}
}
}
}
}
},
{
"operation": "modify-overwrite-beta",
"spec": {
"*": "=size(@(1,&))"
}
},
{
"operation": "shift",
"spec": {
"P": "No\\.of People",
"M": "No\\.of Males",
"F": "No\\.of Females"
}
}
]
其中P
是代表在通用列表下组合所有性别的字母。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.