Back to Code Snippets
Query JSON files Using SQL in PythonPython
DuckDB supports querying JSON files directly, enabling seamless analysis of semi-structured data. This script lets you apply SQL queries to JSON files within a Python environment, ideal for preprocessing or exploring JSON datasets.
Execute this Python
import duckdb def query_json(file_path, query): """ Query JSON data directly using DuckDB. Args: file_path (str): Path to the JSON file. query (str): SQL query to execute on the JSON data. Returns: pandas.DataFrame: Query results as a Pandas DataFrame. """ con = duckdb.connect() # Execute the query on the JSON file and fetch the results as a Pandas DataFrame. df = con.execute(f"SELECT * FROM read_json_auto('{file_path}') WHERE {query}").df() return df # Example Usage result = query_json("./json/query_20min.json", "scheduled = true") print(result)
Copy code