Technical blog
Restricting Returned Fields in an OData Service for Expanded Entities
created by DALL-E
Vlado Balko
7. 8. 2024
Development
When working with OData services, it’s often necessary to limit the amount of data returned by the service to improve performance and reduce payload size. This is particularly useful when dealing with expanded entities, where the potential for large amounts of data is significant. Here’s a concise guide on how to restrict returned fields in an OData service, especially for expanded entities.
Example OData Request
Below is an anonymized and simplified OData request that demonstrates how to filter and select specific fields, including those in an expanded entity, and ensures the response format is JSON:
Breakdown of the Request
1. Base Entity Filtering and Selection:
Filtering: The request filters the base entity (
ZFI_C_XF_EXAMPLE_H
) to only include entries whereexample_id
equals${context.data.example_id}
andfiscal_year
equals${context.data.fiscal_year}
.Selecting Specific Fields: The
$select
parameter specifies that only certain fields (example_id
,example_type
,created_by
,fiscal_year
) should be included in the response.
2. Expanding and Selecting Fields from Related Entities:
Expanding: The
$expand
parameter is used to include related entities (_XFExampleDetails
) in the response.Selecting Specific Fields from Expanded Entities: Within the
$expand
parameter, the$select
clause specifies which fields from the expanded entity should be included (company_code
,amount_local
,due_date
,document_number
)
3. Specifying Response Format:
Response Format: The
$format=json
parameter ensures the response is in JSON format, making it easier to handle in modern web applications.
Example Output JSON
The following is an example of what the output JSON might look like for the above request:
Focus on $expand
and $select
Using the $expand
parameter allows you to include related entities within the main entity's response. By pairing $expand
with $select
, you can precisely control which fields from the related entities are included. This combination is powerful for optimizing the data returned by the service, ensuring you only retrieve necessary information, which can significantly enhance performance and readability of the payload.
Here’s the key part of the request focusing on the $expand
and $select
parameters:
$expand=_XFExampleDetails($select=company_code,amount_local,due_date,document_number)
$expand
: This part ensures that the related entity_XFExampleDetails
is included in the response.$select
within$expand
: Specifies which fields from_XFExampleDetails
should be included, thus filtering out unnecessary data and optimizing the response payload.