Menu

Working with JSON and Text Files

Working with JSON and Text Files

Not all datasets are organized as conventional spreadsheets. JSON is widely used by web applications and APIs, while text files may contain structured or unstructured information.

Loading JSON Data

JSON (JavaScript Object Notation) represents information using objects, arrays, and key-value pairs.

Consider a file named products.json:

[

{"product": "Laptop", "price": 55000},

{"product": "Phone", "price": 30000},

{"product": "Tablet", "price": 25000}

]

You can load this file into a DataFrame with Pandas.

import pandas as pd

df = pd.read_json("products.json")

print(df)

When JSON contains nested objects, the structure may need to be flattened before it can be analyzed as a table. The json_normalize() function helps convert nested records into a tabular format.

import json

import pandas as pd

with open("products.json", "r", encoding="utf-8") as file:

records = json.load(file)

df = pd.json_normalize(records)

print(df)

For simple JSON arrays of objects, read_json() is usually sufficient. json_normalize() is especially useful when working directly with nested Python dictionaries and lists.

Reading a Text File

Python's built-in file handling supports reading text without requiring Pandas.

with open("notes.txt", "r", encoding="utf-8") as file:

content = file.read()

print(content)

You can also read a file line by line.

with open("notes.txt", "r", encoding="utf-8") as file:

for line in file:

print(line.strip())

This approach is useful for processing logs, reports, and other text-based sources before extracting information for analysis.


Python for Data Science

VA

Vishalini A



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details