简体   繁体   English

寻求有关在 python 中将数据从嵌套 JSON 转换为平面 json 的帮助

[英]seeking help regarding converting data from nested JSON to flat json in python

I am looking for converting a nested json into flat json using python.我正在寻找使用 python 将嵌套的 json 转换为平面 json。 I have the data coming from an API response, the number of keys/columns can be upto 100, and the rows/overall count of elements can be 100k我有来自 API 响应的数据,键/列的数量可以达到 100,并且元素的行/总计数可以是 100k

[{"Name":"John", "Location":{"City":"Los Angeles","State":"CA"}},{"Name":"Sam", "Location":{"City":"Chicago","State":"IL"}}]

I did came across this ( Python flatten multilevel JSON ) but this flattens the whole JSON completely, as a result everything falls under one line which I am not looking for currently.我确实遇到过这个( Python flatten multilevel JSON ),但这完全扁平化了整个 JSON,结果一切都归于我目前没有寻找的一行。 I also thought of using this on one the data one array at a time in loop but that is causing a lot of load on the system我也想过在循环中一次一个数组的数据上使用它,但这会给系统造成很大的负载

[{"Name":"John", "City":"Los Angeles","State":"CA"},{"Name":"Sam", "City":"Chicago","State":"IL"}]

Use unpacking with dict.pop :使用dict.pop解包:

[{**d.pop("Location"), **d} for d in l]

Output:输出:

[{'City': 'Los Angeles', 'Name': 'John', 'State': 'CA'},
 {'City': 'Chicago', 'Name': 'Sam', 'State': 'IL'}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM