简体   繁体   English

使用 Pandas 将嵌套的 JSON 转换为 CSV

[英]Convert Nested JSON to CSV using Pandas

I am trying to convert nested JSON into CSV using pandas.我正在尝试使用 Pandas 将嵌套的 JSON 转换为 CSV。 I have viewed similar questions asked here but I can't seem apply in on my scenario.我看过这里提出的类似问题,但我似乎无法应用于我的场景。 My JSON is the following我的 JSON 如下

{
 "51% FIFTY ONE PERCENT(PWD)" : {
 "ID" : "51%1574233975114-WEBAD",
 "contactName" : "",
 "createdAt" : 1574233975,
 "debit" : 118268.19999999995,
 "defaultCompany" : "",
 "emailAddress" : "",
 "lastUpdatedAt" : "",
 "phoneNumber" : "",
 "taskNumber" : 0
},
 "51% STORE (MUZ)" : {
 "ID" : "51%1576650784631-WEBAD",
 "contactName" : "",
 "createdAt" : 1576650784,
 "debit" : 63860,
 "defaultCompany" : "",
 "emailAddress" : "",
 "lastUpdatedAt" : "",
 "phoneNumber" : "",
 "taskNumber" : 0
},
 "ABBOTT S" : {
  "STORE (ABD)" : {
   "ID" : "ABB1574833257715-WEBAD",
   "contactName" : "",
   "createdAt" : 1574833257,
   "debit" : 35065,
   "defaultCompany" : "",
   "emailAddress" : "",
   "lastUpdatedAt" : "",
   "phoneNumber" : "",
   "taskNumber" : 0
 }
}
}

This is a snippet of the JSON and as you can see some entries, not all, are nested.这是 JSON 的一个片段,正如您所看到的,有些条目(并非全部)是嵌套的。 I tried using the json_normalize the following way ie我尝试使用 json_normalize 以下方式即

import json
from pandas.io.json import json_normalize  

with open('.\Customers\kontrolkotlin-CUSTOMERS-export.json') as f:
d = json.load(f)

nycphil = json_normalize(data = d)
nycphil

And got a single row dataframe as output as shown below并得到一个单行数据帧作为输出,如下所示在此处输入图片说明 This doesn't seem to work out as I want to something readable and understandable.这似乎不起作用,因为我想要一些可读和可以理解的东西。

You can take direct data like:您可以获取直接数据,例如:

nycphil = json_normalize(d['51% STORE (MUZ)'])
nycphil.head(3)
print(nycphil.head(3))

在此处输入图片说明

Or try to do something like this或者尝试做这样的事情

df = read_json('some.json')
df.to_csv() 
print(df)

output输出在此处输入图片说明

I'm sure there's a simpler say, but...我敢肯定有一个更简单的说法,但是......

If you assume that the leafs of your nested JSON all have the same fields ( ID , contactName , etc...), then you can recursively flatten your JSON and create a list of records, keeping the path that took you to the leaf.如果您假设嵌套 JSON 的叶子都具有相同的字段( IDcontactName等...),那么您可以递归地展平您的 JSON 并创建一个记录列表,保留将您带到叶子的路径。

Something like:就像是:

def flatten_json(x, path="", result=None):
    if result is None:
        result=[]
    if "ID" in x:
        result.append({**x, "path": path})
        return
    for key in x:
        flatten_json(x[key], path + "/" + key, result)
    return result

df = pd.DataFrame(flatten_json(data))
print(df)

result:结果:

                       ID contactName   createdAt     debit defaultCompany  \
0  51%1574233975114-WEBAD              1574233975  118268.2                  
1  51%1576650784631-WEBAD              1576650784   63860.0                  
2  ABB1574833257715-WEBAD              1574833257   35065.0                  

  emailAddress lastUpdatedAt phoneNumber  taskNumber  \
0                                                  0   
1                                                  0   
2                                                  0   

                          path  
0  /51% FIFTY ONE PERCENT(PWD)  
1             /51% STORE (MUZ)  
2        /ABBOTT S/STORE (ABD)  

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

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