| by Arround The Web | No comments

Pandas Read JSON

Pandas module can work with different data formats, such as JSON, which stands for JavaScript Object Notation. JSON is a common way of storing and exchanging data throughout the web. Sometimes while working with JSON, users need to read the JSON data in Python. For this purpose, the “read_json()” of the “pandas” module is employed in Python.

This tutorial presented a detailed guide on the “pandas.read_json()” method using the following contents:

What is the Python “pandas.read_json()” Method?

In Python, the “pandas.read_json()” method is used to read the JSON string or JSON file and retrieve the Pandas DataFrame or Series object.

Syntax

pandas.read_json(path_or_buf, *, convert_axes=None, orient=None, convert_dates=True, keep_default_dates=True, precise_float=False, typ='frame', dtype=None, date_unit=None, encoding=None, encoding_errors='strict', compression='infer', nrows=None, storage_options=None, lines=False, chunksize=None, dtype_backend=_NoDefault.no_default, engine='ujson')

 

Parameters

In this syntax:

  • The “path_or_buf” parameter represents the “JSON” string or file path.
  • The “orient” parameter represents the JSON file format.
  • The “typ” parameter represents the type of object to be recovered.
  • The “encoding” parameter represents the encoding that is used to decode data.
  • The remaining/other parameters are optional and can be employed for various operations. To learn more, you can check/see this official documentation.

Return Value

The “pandas.read_json()” method retrieves the DataFrame or Series.

Before moving to the example, let’s have a look at the following JSON file that is used in the upcoming example:

Example 1: Read JSON File Using the “pandas.read_json()” Method

In the below-provided example code, imported the “pandas” module and used the “pandas.read_json()” method to read the JSON file by accepting the file name as an argument. After that, utilized the “df.to_string()” method to print the entire DataFrame. Here is an example code:

import pandas
df = pandas.read_json('sample.json')
print(df.to_string())

 

The JSON file has been read successfully:

Example 2: Read JSON String Using the “pandas.read_json()” Method

The “pandas.read_json()” method can also be used to read the JSON string. In the below code, the “pandas.read_json()” method takes the JSON string as an argument and reads the JSON string. Take the following code to perform the particular operation:

import pandas
json_str = '{"Name": ["Joseph", "Anna", "Henry"],"Age": [21, 15, 18],"Height": [5.6, 4.5, 5.8],"Grade": ["A+", "B+", "C+"]}'
df = pandas.read_json(json_str)
print(df)

 

As you can see, the JSON string data has been retrieved successfully in DataFrame format:

Note: We can also read the JSON data from the web by passing the URL as a string into the “pandas.read_json()” method.

Example 3: Read Various Orientations of JSON String Utilizing the “pandas.read_json()” Method

The “JSON” string data has various formats that are used while working with JSON. We can control all of these formats using the “orient=” parameter of the “pandas.read_json()” method. These formats include “split”, “table”, “index”, “records”, and “values”.

Index Orientation

In the below code, the JSON string contains the “Index” orientation format. This format contains the index value as the label of the DataFrame column. The “pandas.read_json()” takes the JSON string and the “orient= ‘index’” parameter as an argument to read the index orientation JSON string data:

import pandas
json_string = '{"0":{"Name":"Joseph","Age":10},"1":{"Name":"Anna","Age":15},"2":{"Name":"Henry","Age":17}}'
df = pandas.read_json(json_string)
print(df, '\n')
df = pandas.read_json(json_string, orient='index')
print(df)

 

The JSON string in index orientation has been read successfully:

Columns Orientation

The “columns” orientation of JSON string data is very equivalent to a Python dictionary. In this format, the columns are the key, and values are also the dictionary with index keys and their values. The below code uses the “pandas.read_json()” method to read this format of JSON:

import pandas
json_string = '{"Name":{"0":"Joseph","1":"Anna","2":"Henry"},"Age":{"0":10,"1":15,"2":17}}'
df = pandas.read_json(json_string)
print(df)

 

According to the provided output, the JSON string data has been read successfully:

Using Values Orientation

The JSON string data can also be represented as a list of lists using the “values” orientation. In this orientation, the column labels are passed separately using the “df.columns” attributes. The following code reads the JSON string in “values” format v using the “pandas.read_json()” method:

import pandas
json_string = '[["Joseph",13,5.3],["Anna", 15,5.6],["Henry",17,4.3]]'
df = pandas.read_json(json_string)
df.columns = ['Name', 'Age', 'Height']
print(df)

 

Output

Some of the other JSON orientations are provided below:

Records Orientation

json_string = '[{"Name":"Joseph","Age":13},{"Name":"Anna","Age":15},{"Name":"Henry","Age":17}]'

 

Split Orientation

json_string = '{"columns":["Name","Age"],"index":[0,1],"data":[["Joseph",13],["Anna",22],["Henry",17]]}'

 

Conclusion

The “pandas.read_json()” method of the “pandas” module is used to read the JSON file or JSON string and retrieve Pandas DataFrame. The JSON data is stored in various orientation formats, such as “records”, “values”, “columns”, and others. The “pandas.read_json()” can read all of these formats easily and efficiently with the help of the “orient=” parameter. This guide delivered a complete tutorial on “pandas.read_json()” via multiple examples.

Share Button

Source: linuxhint.com

Leave a Reply