我一直在试图弄清楚如何从这个 JSON 的事件数组中的数组中获取第一个值。
我想我必须对 Cardinality 做一些事情,但我无法弄清楚,因此我们将不胜感激。
这就是我的 JSON 的样子
[
{
"operation": "shift","spec": {
"*": "&","events": {
"*": {
"*": {
"@id": {
"e0": { "@(2,value)": "&" },"e4": { "@(2,value)": "&" }
}
}
}
},"ids": {
"*": {
"@value": "@id"
}
}
}
}
]
我的 Jolt 规格
{
"rootid" : "19718","clloadm" : "2021-06-01T22:40:02","clload" : "2021-06-01T21:21:39","date" : "2021-05-25T21:52:30","e0" : [ "2021-05-29T08:08:19","2021-05-29T08:08:19" ],"e4" : [ "2021-05-29T08:08:19","id" : "value","new_id" : "value"
}
我得到的结果:
{
"rootid" : "19718","e0" : "2021-05-29T08:08:19","e4" : "2021-05-29T08:08:19","new_id" : "value"
}
我希望结果更像这样,只选择第一个值:
[
{
"operation": "shift","e4": {
"operation": "cardinality","spec": {
"e4": "ONE"
}
}
}
}
}
},"ids": {
"*": {
"@value": "@id"
}
}
}
}
]
编辑: 我已经尝试将基数添加到我的规范中,但我似乎无法得到我想要的结果。
## Assuming that the target data is always under sections[i].payload
from json import loads
line = open("data.json").read()
data = loads(line)["sections"]
for x in data:
try:
# With assumption that there is only one payload
if x["payload"][0]["url"]:
print(x["payload"][0]["url"])
except KeyError:
pass