我在正确完成这项工作时遇到了一些困难,但我有如下数据:
{
"completedProtocol": "Extract",
"map": [
{
"sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024254" }, { "clarityId": "claritySample1", "espId": "ESP024255" }, { "clarityId": "claritySample1", "espId": "ESP024256"}],
"sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
"files":["http://fileserver.net/path/to/datafile3"]
}
],
"map": [
{
"sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024258" }, { "clarityId": "claritySample1", "espId": "ESP024259" }, { "clarityId": "claritySample1", "espId": "ESP024260"}],
"sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
"files":["http://fileserver.net/path/to/datafile3"]
}
]
}
我想把它转换成:
[{"map": [
{
"sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024254" }, { "clarityId": "claritySample1", "espId": "ESP024255" }, { "clarityId": "claritySample1", "espId": "ESP024256"}],
"sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
"files":["http://fileserver.net/path/to/datafile3"]
}
]},
{"map":[
{
"sampleIDsIn": [{ "clarityId": "claritySample1", "espId": "ESP024258" }, { "clarityId": "claritySample1", "espId": "ESP024259" }, { "clarityId": "claritySample1", "espId": "ESP024260"}],
"sampleIDsOut": ["claritySample3", "claritySample4", "claritySample5"],
"files":["http://fileserver.net/path/to/datafile3"]
}
]}]
到目前为止,我的代码是:
import json
obj = json.loads(body)
newData = [dct for dct in obj if 'map' in dct]
但这只会带来:
[u'map']
如果我在主体上使用json.loads
,它只返回map
的第二个值,覆盖第一个值。你知道吗
注意:我想要一系列的单项dict;我不想在单个键下收集值。你知道吗
有什么想法吗?你知道吗
您可以使用自定义
object_pairs_hook
函数强制json.loads()
返回单个项目dict的列表,而不是覆盖重复键的单个dict:从docs:
用法:
在您的例子中,由于您显然对
"map"
键以外的任何东西都不感兴趣,因此可以在之后过滤结果:…这会让你得到你问题中指定的结果。你知道吗
相关问题 更多 >
编程相关推荐