简体   繁体   English

使用 python pandas 将 Json 转换为 csv

[英]Convert a Json to csv using python pandas

I have a json file in the below format that I need to convert to csv in python.我有一个以下格式的 json 文件,我需要在 python 中将其转换为 csv。

{
  "data": {
    "clientApplicationRsaKeyss": [
      {
        "clientId": "abc-efg",
        "createdOn": "2021-02-26T08:45:43.2746397Z"
      },
      {
        "clientId": "xyz-lmn",
        "createdOn": "2022-05-23T16:11:59.435729Z"
      },
]
}
}

I need to convert the above into a csv with the below fields:我需要将以上内容转换为具有以下字段的 csv:

clientID          createdon
abc-efg          2021-02-26T08:45:43.2746397Z
xyz-lmn          2022-05-23T16:11:59.435729Z

How do I do this using python / dataframe?如何使用 python / dataframe 执行此操作?

Its easy to do它很容易做到

>>> df = pd.DataFrame(json.load(open("test.json"))["data"]["clientApplicationRsaKeyss"])
>>> df 
  clientId                     createdOn
0  abc-efg  2021-02-26T08:45:43.2746397Z
1  xyz-lmn   2022-05-23T16:11:59.435729Z
>>> df.to_csv(".....", index=False)

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

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