cancel
Showing results for 
Search instead for 
Did you mean: 

How to extract data from a JSONModel object in SAP UI5

former_member798
Employee
Employee
0 Kudos
var oUserModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser");

//By this i am getting the data of the current user.

console.log(oUserModel)


//when i print it in console I get the currentUser data in the oData Section of the object.Can I know how to extract the data.

former_member798
Employee
Employee
0 Kudos

Thank you c9f8de6ef0704cac8b4246c3c6e79ac1 ,but it shows a blank object which does not contain any data.

Accepted Solutions (0)

Answers (3)

Answers (3)

manju537449
Participant
0 Kudos

Hi Amit,

You can get JSON data by this way.

var oUserModel = new sap.ui.model.json.JSONModel("/services/userapi/currentUser"); 
var data = JSON.parse(oUserModel.getJSON());

Regards,

Manjunatha Devadiga

former_member798
Employee
Employee
0 Kudos

Hi Manjunatha

I am getting the data in the console log ,but unable to print it in console using console.log ();

The output is coming as undefined.

Amit

manju537449
Participant
0 Kudos

Hi Amit,

Try this in your .js file

console.log(data.d) // if the response is object
devendervb
Contributor
0 Kudos

Amit,

If you want to view data before databinding.

You should loaddata into json using

var oUserModel =new sap.ui.model.json.JSONModel();
oUserModel.loadData("/services/userapi/currentUser");
//now check data 
console.log(oUserModel.getData()); 

Hope helps.

smarchesini
Active Contributor
0 Kudos

Hi Amit,

ok sorry, I thought the service work.

For information about user I'm using this :

sap.ushell.Container.getUser()


without json model.

Sebastiano

former_member798
Employee
Employee
0 Kudos

Hi Sebastiano,

There is an Error: Container is unknown.

"Failed to load component for container container. Reason: TypeError: Cannot read property 'Container' of undefined"

var us = new sap.ushell.Container.getUser();
			console.log(us);

Amit

smarchesini
Active Contributor
0 Kudos

The debugger show all the function you can call with sap.ushell.Container.getUser().


In the code use (for example) for get the name:

var sName = sap.ushell.Container.getUser().getFirstName();


Ok ? 🙂