Home > Blockchain >  Convert a Json to csv using python pandas
Convert a Json to csv using python pandas

Time:07-13

I have a json file in the below format that I need to convert to csv in python.

{
  "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:

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?

CodePudding user response:

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)
  • Related