[英]Selective flattening of JSON in Python
I'm extracting data from an API which returns non-standardised jsons, meaning that I have to write a custom function to parse each data stream as the JSON has a different structure of nested dictionaries and lists.我正在从返回非标准化 json 的 API 中提取数据,这意味着我必须编写一个自定义函数来解析每个数据流,因为 JSON 具有不同的嵌套字典和列表结构。 The output I desire is a flattened list containing all of the relevant endpoint items.
我想要的输出是一个包含所有相关端点项的扁平列表。 I've written functions for a few of these but am trying to create something more generalisable.
我已经为其中一些编写了函数,但我正在尝试创建更通用的函数。
An example JSON would be of the form: JSON 示例的格式如下:
JSON = {
'metadata' : {
'meta_param' : 'meta_value',
},
'contents' : [
{
'content' : [
{
'attributes' : {
'series_name' : 'series_1a',
},
'data' : {
'index' : [0, 1, 2, 3, 4, 5],
'values' : [12, 84, 38, 3, 92, 67]
},
},
{
'attributes' : {
'series_name' : 'series_2a',
},
'data' : {
'index' : [0, 1, 2, 3, 4, 5],
'values' : [42, 73, 48, 20, 19, 61]
},
},
],
},
{
'content' : [
{
'attributes' : {
'series_name' : 'series_1b',
},
'data' : {
'index' : [0, 1, 2, 3, 4, 5],
'values' : [13, 85, 39, 4, 93, 68]
},
},
{
'attributes' : {
'series_name' : 'series_2b',
},
'data' : {
'index' : [0, 1, 2, 3, 4, 5],
'values' : [43, 74, 49, 21, 20, 62]
},
},
],
},
]
}
And I'd write a custom function like this to convert it into a flat list我会写一个这样的自定义函数来将它转换成一个平面列表
flatlist = []
for contents in JSON['contents']:
for content in contents['content']:
flatlist.append(content['data'])
flatlist ->
[{'index': [0, 1, 2, 3, 4, 5], 'values': [12, 84, 38, 3, 92, 67]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [42, 73, 48, 20, 19, 61]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [13, 85, 39, 4, 93, 68]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [43, 74, 49, 21, 20, 62]}]
I'm trying to create a function which accepts a JSON of nested dictionaries and lists, as well as a 'routemap' of how to access specific items within it, and then returns a flat list of the specified items.我正在尝试创建一个函数,该函数接受嵌套字典和列表的 JSON,以及如何访问其中特定项目的“路线图”,然后返回指定项目的平面列表。
A routemap and its usage for the previous example would look like:上一个示例的路由图及其用法如下所示:
JSON_route = [
'contents',
'content',
'data',
]
flatlist = json_to_flatlist(JSON, JSON_route)
flatlist ->
[{'index': [0, 1, 2, 3, 4, 5], 'values': [12, 84, 38, 3, 92, 67]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [42, 73, 48, 20, 19, 61]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [13, 85, 39, 4, 93, 68]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [43, 74, 49, 21, 20, 62]}]
My key issue is that I'm not sure how to programmatically convert the JSON_route list into the nested for loops needed to extract the data.我的关键问题是我不确定如何以编程方式将 JSON_route 列表转换为提取数据所需的嵌套 for 循环。
Thanks for any help!谢谢你的帮助!
def json_to_flatlist(obj, route):
if len(route) == 0:
yield obj
elif isinstance(obj, list):
for item in obj:
yield from json_to_flatlist(item, route)
else:
yield from json_to_flatlist(obj[route[0]], route[1:])
JSON_route = [
'contents',
'content',
'data',
]
result = list(json_to_flatlist(JSON, JSON_route))
print(result)
Result (newlines added for clarity):结果(为清楚起见添加了换行符):
[
{'index': [0, 1, 2, 3, 4, 5], 'values': [12, 84, 38, 3, 92, 67]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [42, 73, 48, 20, 19, 61]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [13, 85, 39, 4, 93, 68]},
{'index': [0, 1, 2, 3, 4, 5], 'values': [43, 74, 49, 21, 20, 62]}
]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.