Search code examples
odata

Expand and Filter OData


I am trying to build a GET request to only pull specific data but I am having problems with utilizing OData. Objects pulled from the API looks like this:

    {
    "PostalCode": "48103",
    "FranchiseId": 397,
    "Franchise": {
        "ConceptId": 1,
        "CreatedDateTime": "2015-10-20T18:36:46.353",
        "ModifiedDateTime": "2018-01-26T14:35:33.447",
        "DeletedDateTime": null,
        "DoingBusinessAs": "Appliance.inc",
        "Status": "Active",
        "IsOwned": true,
        "FranchiseeName": "Appliance Inc - Ann Arbor, MI - MLY 1001",
        "LicenseNumber": "ML89023",
        "City": "Ann Arbor",
        "StateAbbr": "MI",
        "PostalCode": "48104",
        "ContactFirstName": "Doug",
        "ContactLastName": "Smith",
        "Country": "United States"
    }
},

I am doing this GET request to get that data:

#url#&$expand=Franchise&$select=FranchiseId, PostalCode

I am using &$expand=Franchise just because I need the "FranchiseeName" from Franchise object but I don't want to pull the rest of the data of that object. How can I write the request so I only pull three fields: PostalCode, FranchiseId, FranchiseeName. The object should look something like this:

{
"PostalCode": "48103",
"FranchiseId": 397,
"FranchiseeName": "Appliance.inc"
}

Solution

  • I assume #url# is a placeholder for the OData URI to your entity, e.g. http://server/odata/entityName.

    The following URL should get the data you need:

    http://server/odata/entityName?$select=PostalCode,FranchiseId&$expand=Franchise($select=FranchiseeName)

    That should return only the required data, but still having the same structure as the full data, e.g:

    {
      "PostalCode": "48103",
      "FranchiseId": 397,
      "Franchise": {
        "FranchiseeName": "Appliance.inc"
      }
    }