Python-如何将S3文件夹转换为JSON层次结构



我使用boto3创建了一个列表,其中包含S3存储桶中的所有子文件夹。列表排序如下:

s3_list = ['a', 'a/a1/a11', 'b', 'b/b1', 'b/b2', 'b/b2/b22']

我正在尝试将此列表转换为JSON层次结构:

{
"root": [
{
"name": "a",
"path": "a",
"child": [
{
"name": "a1",
"path": "a/a1",
"child": [
{
"name": "a11",
"path": "a/a1/a11"
}
]
}
]
},
{
"name": "b",
"path": "b",
"child": [
{
"name": "b1",
"path": "b/b1"
},
{
"name": "b2",
"path": "b/b2",
"child": [
{
"name": "b22",
"path": "b/b2/b22"
}
]
}
]
}
]
}

在Python中实现这一点的最佳方法/库是什么?

您可以将递归与collections.defaultdict:一起使用

from collections import defaultdict
s3_list = ['a', 'a/a1/a11', 'b', 'b/b1', 'b/b2', 'b/b2/b22']
def to_dict(d, c = []):
if not d:
return {}
_d, r = defaultdict(list), []
for a, *b in d:
_d[a].append(b)
return [{'name':a, 'path':'/'.join(c+[a]), 
**({} if not (k:=list(filter(None, b))) else  {'children':to_dict(k, c+[a])})} 
for a, b in _d.items()]

result = {'root':to_dict([i.split('/') for i in s3_list])}

import json
print(json.dumps(result, indent=4))

输出:

{
"root": [
{
"name": "a",
"path": "a",
"children": [
{
"name": "a1",
"path": "a/a1",
"children": [
{
"name": "a11",
"path": "a/a1/a11"
}
]
}
]
},
{
"name": "b",
"path": "b",
"children": [
{
"name": "b1",
"path": "b/b1"
},
{
"name": "b2",
"path": "b/b2",
"children": [
{
"name": "b22",
"path": "b/b2/b22"
} 
]
}
]
}
]
}

最新更新