This article walks you through querying structured data from inmydata using the StructuredDataDriver
. Structured data queries are extremely fast, making them ideal for building tools that power conversational interfaces—especially when you need to quickly retrieve data from the inmydata platform in real time.
You'll learn how to:
Connect using your tenant
Use a simple filter for basic equality queries
Use a complex filter with logical operators and brackets for more advanced conditions
Before starting, make sure you have:
Set up your environment variables (
INMYDATA_API_KEY
,INMYDATA_TENANT
,INMYDATA_CALENDAR
)Installed the
inmydata
Python package andpython-dotenv
via pip
1. Load Environment Variables
Start by loading your credentials from the .env
file:
python import os from dotenv import load_dotenv load_dotenv()
2. Initialize the Structured Data Driver
Create an instance of the StructuredDataDriver
using your tenant:
python from inmydata.StructuredData import StructuredDataDriver driver = StructuredDataDriver(os.environ['INMYDATA_TENANT'])
3. Use get_data_simple()
for Basic Filtering
Use get_data_simple()
when your filters are simple equality filters with no brackets or OR logic.
Example: Filter sales data for the Edinburgh store
python import os from dotenv import load_dotenv from inmydata.StructuredData import StructuredDataDriver, AIDataSimpleFilter load_dotenv() driver = StructuredDataDriver(os.environ['INMYDATA_TENANT']) # -- Use get_data_simple when your filter is simple (only equality filters, no bracketing, no ORs, etc.) # Build our simple filter filter = [] filter.append( AIDataSimpleFilter( "Store", # Field to filter on "Edinburgh") # Value to filter by ) df = driver.get_data_simple( "Inmystore Sales", # Name of the subject we want to extract data from ["Financial Year","Sales Value"], # List of fields we want to extract filter, # Filters to apply False) # Whether filters are case sensitive print(df)
4. Use get_data()
for Complex Filtering
Use get_data()
when your filter requires:
Multiple conditions
Logical operators like AND/OR
Bracketing for precedence
Non-equality comparisons
In this example, we build a filter to match records where the Store
is either Edinburgh or London. This uses the AIDataFilter
class and logical operators to combine conditions.
import os from dotenv import load_dotenv from inmydata.StructuredData import StructuredDataDriver, AIDataFilter, LogicalOperator, ConditionOperator load_dotenv() driver = StructuredDataDriver(os.environ['INMYDATA_TENANT']) # -- Use get_data when your filter more complex (non-equality matches, bracketing, ORs, etc.) -- # Build our filter filter = [] filter.append( AIDataFilter( "Store", ConditionOperator.Equals, # Condition to use in the filter LogicalOperator.And, # Logical operator to use in the filter "Edinburgh", # Value to filter by 0, # Number of brackets before this condition 0, # Number of brackets after this condition False # Whether the filter is case sensitiv ) ) filter.append( AIDataFilter( "Store", ConditionOperator.Equals, # Condition to use in the filter LogicalOperator.Or, # Logical operator to use in the filter "London", # Value to filter by 0, # Number of brackets before this condition 0, # Number of brackets after this condition False # Whether the filter is case sensitiv ) ) df = driver.get_data( "Inmystore Sales", # Name of the subject we want to extract data from ["Financial Year","Store","Sales Value"], # List of fields we want to extract filter) # Filters to apply print(df)
? This example only builds the filter and prints the
df
object. Make suredf
has been defined via a call toget_data()
beforehand.
Notes
Both
get_data_simple()
andget_data()
return a Pandas DataFrame.Use
get_data_simple()
when possible — it's more readable and easier to construct.Brackets and logical operators in
AIDataFilter
allow for flexible nesting of conditions.
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article