How to read a json-dictionary type file with pandas? How to read a json-dictionary type file with pandas? pandas pandas

How to read a json-dictionary type file with pandas?


The json method doesnt work as the json file is not in the format it expects. As we can easily load a json as a dict, let's try this way :

import pandas as pdimport jsonimport osos.chdir('/Users/nicolas/Downloads')# Reading the json as a dictwith open('json_example.json') as json_data:    data = json.load(json_data)# using the from_dict load function. Note that the 'orient' parameter #is not using the default value (or it will give the same error that you got before)# We transpose the resulting df and set index column as its index to get this resultpd.DataFrame.from_dict(data, orient='index').T.set_index('index')   

output:

                                                                 data columnsindex                                                                        311210177061863424  [25-34\n, FEMALE, @bikewa absolutely the best....     age310912785183813632  [25-34\n, FEMALE, Photo: I love the Burke-Gilm...  gender311290293871849472  [25-34\n, FEMALE, Photo: Inhaled! #fitfoodie h...    text309386414548717569  [25-34\n, FEMALE, Facebook Is Making The Most ...    None312327801187495936  [25-34\n, FEMALE, Still upset about this >&...    None312249421079400449  [25-34\n, FEMALE, @JoeM_PM_UK @JonAntoine I've...    None308692673194246145  [25-34\n, FEMALE, @Social_Freedom_ actually, t...    None308995226633129984  [25-34\n, FEMALE, @seattleweekly that's more t...    None308660851219501056  [25-34\n, FEMALE, @adamholdenbache I noticed 1...    None308658690528014337  [25-34\n, FEMALE, @CEM_Social I am waiting pat...    None309719798001070080  [25-34\n, FEMALE, Going to be watching Faceboo...    None312349448049152002  [25-34\n, FEMALE, @anikamarketer I applied for...    None312325152698404864  [25-34\n, FEMALE, @_chrisrojas_ wow, that's so...    None310546490844135425  [25-34\n, FEMALE, Photo: Feeling like a bit of...    None


the pandas module and not the json module should be the answer:pandas itself has read_json capabilities and the root of the problem must be that you did not read the json in the correct orientation.you must pass the exact orient parameter with which you created the json variable in the first place

ex.:

df_json = globals()['df'].to_json(orient='split')

and then:

read_to_json = pd.read_json(df_json, orient='split')