This JSON structure is from a Spring Cloud server config:
{
"item[0].subitem[0].key": "value1",
"item[0].subitem[1].key": "value2",
"item[1].subitem[0].key": "value3",
"item[1].subitem[1].key": "value4"
}
I have a Flask app which will consume that format, but I need to convert it to this format for further processing:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
},
{
"subitem": [
{
"key": "value1"
},
{
"key": "value1"
}
]
}
]
}
9
The code below shows a generalized solution that includes more test cases than your requirement:
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
becomes:
{'item': [{'subitem': [{'key': 'value1'}, {'key': 'value2'}]},
{'subitem': [{'key': 'value3'}, {'key': 'value4'}]}],
'item2': [{'subitem': ['value5', 'value6']},
[{'key1': 'value7'}, {'key2': 'value8'}]]}
6
or use a package made for this:
https://github.com/amirziai/flatten
pip install flatten_json
which support flattening and unflattening.
1
I explained why I don’t want to provide a complete solution in the comments to the OP’s question. This should be enough of a hint:
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))
Results in:
{
"item": [
{
"subitem": [
{
"key": "value1"
},
{
"key": "value2"
}
]
},
{
"subitem": [
{
"key": "value3"
},
{
"key": "value4"
}
]
}
]
}
1