import json
# Loading json file as dictionary
with open("json_file.json", "r") as file:
raw_stock_data = json.load(file)
# Loading json data as dataframe
import pandas as pd
df = pd.read_json("json_file.json", orient='columns')
# Turning a json into string
json.dumps(json_schema["properties"]) # '{"maximum": 130, "minimum": 1, "type": "integer"}'
# Writing a json file
with open("json_file.json", mode="w") as fh:
json.dump(obj=json_schema, fp=fh)
### Which orient to use:
# split -> Separate index, columns, and data as key-value pairs for dataframe -> {"index": [...], "columns": [...], "data": [...]}
# records -> List of dictionaries (each dict is a row) -> [{"col1": value1, "col2": value2}, ...]
# index -> Dict of dicts (rows are keys) -> {"row1": {"col1": value1, "col2": value2}, ...}
# columns -> Dict of lists (columns are keys) -> {"col1": [values], "col2": [values], ...}
# values -> List of lists (each list is a row) -> [[value1, value2], [value3, value4]]