Skip to content Skip to sidebar Skip to footer

Convert All Data Within The Json To Csv Using Pandas Or Python

I tried using json_normalize to flatten data but a part of the data is still not flattened. How can I get flattened data for this json. Note: This is a sample json file but the dat

Solution 1:

Consider the following demo:

In [105]: json_file = {
     ...:   "data": "abc",
     ...:   "data2": 123,
     ...:   "results": {
     ...:     "name": "w",
     ...:     "more_data": [
     ...:       {
     ...:         "no": "111",
     ...:         "code": 3
     ...:       },
     ...:       {
     ...:         "no": "222",
     ...:         "code": 4
     ...:       }
     ...:
     ...:     ],
     ...:     "id": 1
     ...:   }
     ...: }
     ...:

In [106]:

In [106]: json_normalize(json_file, 
                         [['results','more_data']], 
                         ['data','data2', ['results','id'], ['results','name']],
                         record_prefix='results.more_data.')
Out[106]:
   results.more_data.code results.more_data.no data  data2  results.id results.name
0                       3                  111  abc    123           1            w
1                       4                  222  abc    123           1            w

Post a Comment for "Convert All Data Within The Json To Csv Using Pandas Or Python"