以前我在这里发布了一个问题(从json文件创建一个列表,有多个值)。2然而,我发现我的一些键已经过时了,它们包含一个叫做“end”的键。3我无法过滤掉它们,使得数据不稳定。
下面是我用于测试的更新后的JSON:
[
{
"primary": "JOHN DOE",
"attributes": [
{
"type": "double",
"name": "BUILDING_NUMBER",
"value": "123"
},
{
"type": "double",
"name": "FLOOR",
"value": 10
},
{
"type": "string",
"name": "EMAIL",
"value": "john.doe@contoso.com"
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "SALESMAN"
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "TRAINEE",
"end": "11-09-2022"
}
],
"aliases": [
{
"alias": "joao.doe@contoso.com"
}
]
},
{
"primary": "LORRAINE DOE",
"attributes": [
{
"type": "double",
"name": "BUILDING_NUMBER",
"value": 456
},
{
"type": "double",
"name": "FLOOR",
"value": 10
},
{
"type": "string",
"name": "STATUS",
"value": "Unavaliable"
},
{
"type": "string",
"name": "EMAIL",
"value": "lorraine.doe@contoso.com"
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "Procurement",
"end": "11-09-2021"
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "SECRETARY"
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "SALES",
"end": "11-09-2021"
}
],
"aliases": [
{
"alias": "lorris.doe@contoso.com"
},
{
"alias": "lorris2.doe@contoso.com"
}
]
},
{
"primary": "JACK DOE",
"attributes": [
{
"type": "double",
"name": "BUILDING_NUMBER",
"value": "123"
},
{
"type": "double",
"name": "FLOOR",
"value": 10
},
{
"type": "string",
"name": "JOB_TITLE",
"value": "OWNER"
}
],
"aliases": [
{
"alias": "jack.doe@contoso.com"
},
{
"alias": "jackson.doe@contoso.com"
}
]
},
{
"primary": "NOAH DOE",
"attributes": [
{
"type": "double",
"name": "BUILDING_NUMBER",
"value": "123"
},
{
"type": "double",
"name": "FLOOR",
"value": 10
},
{
"type": "string",
"name": "EMAIL",
"value": "noah.doe@contoso.com"
}
],
"aliases": [
{
"alias": "noah.doe95@contoso.com"
}
]
}
]
是否有办法过滤掉关键字为“end”的属性,并使结果如下所示?
"john.doe@contoso.com": "SALESMAN",
"lorraine.doe@contoso.com": "SECRETARY"
这是老办法:
.[].attributes | from_entries
| select(has("JOB_TITLE") and has("EMAIL"))
| "\"\(.EMAIL)\": \"\(.JOB_TITLE)\""
2条答案
按热度按时间fdbelqdn1#
在运行
from_entries
之前,使用map
将它们过滤掉:第一个
Demo
tjrkku2a2#
在将属性转换为对象之前,请仅选择那些不具有
end
特性的对象:输出量: