Skip to Content

Problem parsing oData v2 response

Hello Experts,

I'm using a custom oData service to get employeeNumber(s) from userID (mapped to GET_ENTITYSET).

Entity structure is very simple (Employee):


EmployeeNumber (key)

A UserID is sent to the service and Pernr(s) are sent from the service.

SAPUI5 code:

        var oModel = new sap.ui.model.odata.v2.ODataModel(sRoot + "/sap/opu/odata/sap/ZOMRI_HR1_SRV");
        oModel.attachRequestCompleted(function() {
            var k = Object.keys(this.oData)[0]; // Works but I don't think this is proper code
            var employeeNumber= this.oData[k].EmployeeNumber;

        var aFilters = [];
        var nameFilter = new sap.ui.model.Filter("UserID", sap.ui.model.FilterOperator.EQ, "OMRICOH");
        var mParameters = {
                  filters : aFilters //,
                 //urlParameters:{"$format" : "json"} // Not needed (default in v2)
        };"/Employees", mParameters);

In the above example input parameter name is 'UserID' and value is 'OMRICOH'

Output looks like this (in SAP Gateway Client)


  "d" : {

    "results" : [


        "__metadata" : {

          "id" : "http://<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')",

          "uri" : "http://<server:port>/sap/opu/odata/sap/ZOMRI_HR1_SRV/Employees('01000659')",

          "type" : "ZOMRI_HR1_SRV.Employee"


        "UserID" : "",

        "EmployeeNumber" : "01000659"





I need to get to the 'EmployeeNumber' value.

The problem is that when I try to get the output in a simple way:

1) I tried o.getParameter("response").responseText in 'attachRequestCompleted' function:


'responseText' is a String and not an object...

2) The following code works:

var k = Object.keys(this.oData)[0];

var employeeNumber= this.oData[k].EmployeeNumber;

However the main problem is that the response object contains the required value in the object key, this.oData returns (from 'attachRequestCompleted' function):

Object {Employees('01000659'): Object}

--Employees('01000659'): Object

----EmployeeNumber: "01000659"

----UserID: ""

So what am I missing?

What do I need to change the SAPUI5 code in order to parse the response correctly or does the problem come from the GW side?

SAPUI5 version is 1.28.7



Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

1 Answer

  • Best Answer
    avatar image
    Former Member
    Oct 16, 2015 at 09:20 AM


    I have two suggestions that you can try, but I don't know if they will work since I can't see the data that you are receiving.

    Option 1:

    Once you get the response text, use

    var responseObject = JSON.parsere(responseText);

    Then access your data through the responseObject.

    Option 2:

    I don't know if using attachRequestCompleted is a requirement for you, but if it's not then you can remove it and rather have the callBack function defined directly in your request. For example:

    var mParameters = {
       filters: aFilters,
      success: function(oData, oResponse){
    };"/Employees", mParameters);

    the oData parameter should then contain the returned data, but you can also try and access it in oResponse. As I can't inspect the data myself, I can't say for sure though. Take a look at JsDoc Report - SAP UI development Toolkit for HTML5 - API Reference - sap.ui.model.odata.v2.ODataModel for more information on it.

    Hope this is helpful


    Add comment
    10|10000 characters needed characters exceeded

    • Hi Albert,

      Option #1 Solved the problem.

      In 'attachRequestCompleted' function:

      var oResponse = JSON.parse(o.getParameter("response").responseText);
      var aResult = oResponse.d.results;
      for (var i=0; i<aResult.length; i++) {