Skip to Content
Author's profile photo Former Member

Extending a control in the SAPUI5 library: sap.ui.model.json.JSONModel

Extending a control in the SAPUI5 library: sap.ui.model.json.JSONModel

(Written based on version 1.28 of SAPUI5)

On a recent project building SAPUI5 application to be hosted on HCP (HANA Cloud Platform) we had a requirement to interface with RESTful services exposed by PI. Being a Gateway and OData girl myself this was a new challenge. “At least a REST service is better than a SOAP service”, I thought. “But how can I load the data in, in a neat and tidy way, like I would with a OData service?” The answer: extend the standard JSONModel, and bend it to my will!


  1. To be able to load data from a single service into the model without overwriting everything already in it
    1. The “loadData” function already available in the JSONModel could not be used to fulfil this requirement as it sets the response of the request at the root of the model, overwriting any existing data
  2. To load data onto a specified path of the model, in order to allow binding in a view to the correct path before the data is loaded in
  3. To be able to specify whether the structure of the data loaded in should be an array or not
    1. An oddity of PI, or more likely the underlying services it is communicating with, seems to be that if a request for a set of data only finds a single item, it will return an object, rather than an array with one object in it
  4. To be able to bind only specific properties of the response to the model
    1. E.g. the response data is in the following structure, we only want to bind to “Items” node to our model
      1. “d” : { “results” : { “Items” : [ “a” :  ] } }

Overall, I wanted to make my JSONModel “feel” more like an ODataModel.


  1. Extend the JSONModel control
  2. On instantiation of the model (in the constructor function) include a service URL parameter on which to base all calls subsequently made against the model
  3. Add a new function within the model for loading the data
  4. Add functions to the model to allow “create”, “update” and “remove”

In order to write the code around this I referred to the ODataModel code base, and stole relevant chunks where rewriting them would have been unnecessary.

As the code would be useful across many applications I hosted it on HCP as an HTML5 application called “shared”, even though it is simply a collection of files rather than an executable application. In this way I can now access the extended JSONModel code from other applications hosted on the same HCP by adding a new destination, of type “application”, into the neo-app.json file, and adding a new resource root into the index file.

Please see my GitHub repository, sapui5-shared, for the source code of my extended JSONModel.

Example index.html resource root

        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta charset="UTF-8">
<title>Bluefin Application One</title>
        <script id="sap-ui-bootstrap"
" bluefin.application1": "./",
"bluefin.shared": "./shared"
            sap.ui.getCore().attachInit(function() {
                new sap.ui.core.ComponentContainer({
height : "100%",
name : "bluefin.application1"
    <body class="sapUiBody" id="content">

Example neo-app.json file

"welcomeFile": "index.html",
  "routes": [
"path": "/resources",
"target": {
"type": "service",
"name": "sapui5",
"entryPath": "/resources"
"description": "SAPUI5 Resources"
"path" : "/shared",
"target" : {
"type" : "application",
"name" : "shared"
"description" : "Shared Utilities"
"path": "/Search",
"target": {
"type": "destination",
"name": "myHCPDestination",
"entryPath" : "/Search"
"description": "Search UI5 destination"

Example code-snippet for use of bluefin.shared.model.JSONModel.loadDataFromPath"bluefin.shared.model.JSONModel");
var oMyModel = new bluefin.shared.model.JSONModel("/Search");
// Read the booking data from the service

sModelPath : "/Bookings",
aResponseProperty : [ "d", "results"],
bReturnArray : true
// Array of data can now be found on the model
var aBookings = oMyModel.getProperty("/Bookings");


The code for the extended JSON Model can be found in the model folder of my sapui5-shared repository.

Assigned Tags

      You must be Logged on to comment or reply to a post.
      Author's profile photo Steven De Saeger
      Steven De Saeger

      Nice !

      Author's profile photo Kedar Tingikar
      Kedar Tingikar

      Hi Lindsay,

      Good info and a differet approach to resolve the issue.

      I had a question though, What is the use of neo-app.json file?

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Good question!

      neo-app.json is used in applications which are hosted on HCP (HANA Cloud Platform). It is used to map to destinations on HCP (set-up in the "Destinations" section of HCP which connect to services), resources (the UI5 library) and other applications hosted on the same platform, as other HTML5 applications.

      This documentation may be helpful to you to understand better: SAPUI5 SDK - Demo Kit

      Thanks 🙂

      Author's profile photo Former Member
      Former Member

      To be able to load data from a single service into the model without overwriting everything already in it

      fyi, loadData has a flag bMerge which can be set to true if the data has to merged instead of override.


      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks Sakthivel! I've never used the merge parameter on the JSON model before. Does this mean if you make an entirely different "loadData" request, i.e. to a new path on a new service, with bMerge = true, this data will be added to the JSON model as a new property node?

      Thanks for sharing this 🙂 I have learnt something new!

      Author's profile photo P. Willemsen
      P. Willemsen

      Nope, it calls jQuery.extend() using the existing data and the loaded data.

      Author's profile photo Former Member
      Former Member

      Nice work. This has proven to be extremely useful.

      Author's profile photo Alexandre Costa
      Alexandre Costa

      Nice article, Lindsay!

      Thank you for your input.