使用 Python 展开从 JSON 获得的字典
Unflatten a dict obtained from JSON using Python
此 JSON 结构来自 Spring 云服务器配置:
{
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4"
}
我有一个将使用该格式的 Flask 应用程序,但我需要将其转换为该格式以便进一步处理:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
},
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
}
]
}
下面的代码显示了一个通用解决方案,其中包含比您的要求更多的测试用例:
import re
j = {
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4",
"item2[0].subitem[0]": "value5",
"item2[0].subitem[1]": "value6",
"item2[1][0].key1": "value7",
"item2[1][1].key2": "value8"
}
d = {}
for key, value in j.items():
s = d
tokens = re.findall(r'\w+', key)
for count, (index, next_token) in enumerate(zip(tokens, tokens[1:] + [value]), 1):
value = next_token if count == len(tokens) else [] if next_token.isdigit() else {}
if isinstance(s, list):
index = int(index)
while index >= len(s):
s.append(value)
elif index not in s:
s[index] = value
s = s[index]
d
变为:
{'item': [{'subitem': [{'key': 'value1'}, {'key': 'value2'}]},
{'subitem': [{'key': 'value3'}, {'key': 'value4'}]}],
'item2': [{'subitem': ['value5', 'value6']},
[{'key1': 'value7'}, {'key2': 'value8'}]]}
我在对 OP 问题的评论中解释了为什么我不想提供完整的解决方案。这应该足够提示了:
def main():
input = {
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4",
}
items = list(input.items())
random.shuffle(items)
shuffled = dict(items)
result = normalize_keys(unflatten(shuffled))
print(json.dumps(result, indent=2))
结果:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value2"
}
]
},
{
"subitem": [
{
"key": "value3"
},
{
"key": "value4"
}
]
}
]
}
或使用为此制作的软件包:
https://github.com/amirziai/flatten
pip install flatten_json
支持扁平化和去扁平化。
此 JSON 结构来自 Spring 云服务器配置:
{
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4"
}
我有一个将使用该格式的 Flask 应用程序,但我需要将其转换为该格式以便进一步处理:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
},
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
}
]
}
下面的代码显示了一个通用解决方案,其中包含比您的要求更多的测试用例:
import re
j = {
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4",
"item2[0].subitem[0]": "value5",
"item2[0].subitem[1]": "value6",
"item2[1][0].key1": "value7",
"item2[1][1].key2": "value8"
}
d = {}
for key, value in j.items():
s = d
tokens = re.findall(r'\w+', key)
for count, (index, next_token) in enumerate(zip(tokens, tokens[1:] + [value]), 1):
value = next_token if count == len(tokens) else [] if next_token.isdigit() else {}
if isinstance(s, list):
index = int(index)
while index >= len(s):
s.append(value)
elif index not in s:
s[index] = value
s = s[index]
d
变为:
{'item': [{'subitem': [{'key': 'value1'}, {'key': 'value2'}]},
{'subitem': [{'key': 'value3'}, {'key': 'value4'}]}],
'item2': [{'subitem': ['value5', 'value6']},
[{'key1': 'value7'}, {'key2': 'value8'}]]}
我在对 OP 问题的评论中解释了为什么我不想提供完整的解决方案。这应该足够提示了:
def main():
input = {
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4",
}
items = list(input.items())
random.shuffle(items)
shuffled = dict(items)
result = normalize_keys(unflatten(shuffled))
print(json.dumps(result, indent=2))
结果:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value2"
}
]
},
{
"subitem": [
{
"key": "value3"
},
{
"key": "value4"
}
]
}
]
}
或使用为此制作的软件包:
https://github.com/amirziai/flatten
pip install flatten_json
支持扁平化和去扁平化。