[英]How to append the list of dictionary to same list in python?
I'm having a JSON with nested values.我有一个带有嵌套值的 JSON。 I need to remove the key of the nested field and need to keep the values as plain JSON.我需要删除嵌套字段的键,并需要将值保持为普通 JSON。
JSON (Structure of my JSON) JSON (我的 JSON 的结构)
[
{
"id":"101",
"name":"User1",
"place":{
"city":"City1",
"district":"District1",
"state":"State1",
"country":"Country1"
},
"access":[{"status":"available"}]
}
]
I need to get the JSON output as:我需要得到 JSON output 为:
Expected Output:预期 Output:
[
{
"id":"101",
"name":"User1",
"city":"City1",
"district":"District1",
"state":"State1",
"country":"Country1"
"access":[{"status":"available"}]
}
]
What i need is:我需要的是:
Place
field out of the JSON从 JSON 中取出Place
字段Python Python
for i in range(0,len(json_data)):
place_data = json_data[i]['place']
print(type(place_data)) #dict
del place_data['place']
Any approach to get the expected output in python.?在 python 中获得预期的 output 的任何方法。
One way to accomplish this could be by实现这一目标的一种方法是
for i in json_data:
i.update(i.pop("place"))
Another way to accomplish this with multiple "keys" updated... This would only work for a single nested level as described in the original question另一种通过更新多个“键”来完成此操作的方法......这仅适用于原始问题中描述的单个嵌套级别
def expand(array):
flatten = list()
for obj in array:
temp = {}
for key, value in obj.items():
if isinstance(value, dict):
temp.update(value)
else:
temp.update({key:value})
flatten.append(temp)
return flatten
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.