Search code examples
restodata

Does the OData protocol provide a way to transform an array of objects to an array of raw values?


Is there a way specify in an OData query that instead of certain name/value pairs being returned, a raw array should be returned instead? For example, if I have an OData query that results in the following:

{
  "@odata.context": "http://blah.org/MyService/$metadata#People",
  "value": [
    {
      "Name": "Joe Smith",
      "Age": 55,
      "Employers": [
        {
          "Name": "Acme",
          "StartDate": "1/1/1990"
        },
        {
          "Name": "Enron",
          "StartDate": "1/1/1995"
        },
        {
          "Name": "Amazon",
          "StartDate": "1/1/1999"
        }
      ]
    },
    {
      "Name": "Jane Doe",
      "Age": 30,
      "Employers": [
        {
          "Name": "Joe's Crab Shack",
          "StartDate": "1/1/2007"
        },
        {
          "Name": "TGI Fridays",
          "StartDate": "1/1/2010"
        }
      ]
    }
  ]
}

Is there anything I can add to the query to instead get back:

{
  "@odata.context": "http://blah.org/MyService/$metadata#People",
  "value": [
    {
      "Name": "Joe Smith",
      "Age": 55,
      "Employers": [
        [ "Acme", "1/1/1990" ],
        [ "Enron", "1/1/1995" ],
        [ "Amazon", "1/1/1999" ]
      ]
    },
    {
      "Name": "Jane Doe",
      "Age": 30,
      "Employers": [
        [ "Joe's Crab Shack", "1/1/2007" ],
        [ "TGI Fridays", "1/1/2010" ]
      ]
    }
  ]
}

While I could obviously do the transformation client side, in my use case the field names are very large compared to the data, and I would rather not transmit all those names over the wire nor spend the CPU cycles on the client doing the transformation. Before I come up with my own custom parameters to indicate that the format should be as I desire, I wanted to check if there wasn't already a standardized way to do so.


Solution

  • OData provides several options to control the amount of data and metadata to be included in the response.

    In OData v4, you can add odata.metadata=minimal to the Accept header parameters (check the documentation here). This is the default behaviour but even with this, it will still include the field names in the response and for a good reason.

    I can see why you want to send only the values without the fields name but keep in mind that this will change the semantic meaning of the response structure. It will make it less intuitive to deal with as a json record on the client side.

    So to answer your question, The answer is 'NO',

    Other options to minimize the response size:

    You can use the $value OData option to gets the raw value of a single property. Check this example: services.odata.org/OData/OData.svc/Categories(1)/Products(1)/Supplier/Address/City/$value

    You can also use the $select option to cherry pick only the fields you need by selecting a subset of properties to include in the response