READ JSON DATA IN PANDAS
Listing 4.16 displays the contents of pandas_read_json.py that shows you how to read a JSON string in Pandas.
LISTING 4.16: pandas_read_json.py
from io import StringIO
import pandas as pd
books = '{"name": "SQL Fundamentals"}\n{"name": "SQL Fundamentals"}\n{"name": "Python and Machine Learning"}\n'
json = StringIO(books)
result = pd.read_json(json, lines=True)
print("json:",result)
Listing 4.16 starts with two import statements and then initializes the variable books with a string of name/value pairs. Next, the variable json is initialized as a JSON string based on the contents of books. Then the variable result is initialized by invoking the Pandas read_json() method with the variable json, after which its contents are printed. Now launch the code in Listing 4.16 and if everything worked correctly you will see the following output:
XML data:
name
0 SQL Fundamentals...