使用下面的代码,我可以通过一个键Map一个字典列表
import json
values_list = [{"id" : 1, "user":"Rick", "title":"More JQ"}, {"id" : 2, "user":"Steve", "title":"Beyond"}, {"id" : 1, "user":"Rick", "title":"Winning"}]
result = {}
for data in values_list:
id = data['id']
user = data['user']
title = data['title']
if id not in result:
result[id] = {
'id' : id,
'user' : user,
'books' : {'titles' : []}
}
result[id]['books']['titles'].append(title)
print(json.dumps((list(result.values())), indent=4))
了解Jolt Spec有多干净,并尝试将模式分离到代码之外。有没有一种方法可以使用Jolt Spec来实现相同的结果?
结果
[
{
"id": 1,
"user": "Rick",
"books": {
"titles": [
"More JQ",
"Winning"
]
}
},
{
"id": 2,
"user": "Steve",
"books": {
"titles": [
"Beyond"
]
}
}
]
1条答案
按热度按时间jchrr9hc1#
可以使用三个级别的连续规格
id
值由"@(1,id)."
表达式组合1
,2
)