Load JSON String into Pandas DataFrame

Let us see how can we use a dataset in JSON format in our Pandas DataFrame. This can be done using the built-in read_json() function. It enables us to read the JSON in a Pandas DataFrame.

Example : Consider the JSON file path_to_json.json :

path_to_json.json

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas
  
# reading the file
data = df.read_json("path_to_json.json")
  
# displaying the DataFrame
print(data)

chevron_right


Output :

Now, the final data frame also depends on the type of the JSON file. So there are mainly 3 types of orientations in JSON : 



  • Index Oriented
  • Value-Oriented 
  • Column Oriented

1. Index Oriented

This is an example of an Index Oriented JSON file. 

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

chevron_right


Output :

2. Value-Oriented

This is an example of a Value-Oriented JSON file.



filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

chevron_right


Output :

3. Column Oriented

This is an example of a Column-oriented JSON file.

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing the module
import pandas
  
# reading the file
data = df.read_json("sample.json")
  
# displaying the DataFrame
print(data)

chevron_right


Output :

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.