Skip to Content

Utilizing the most recent HANA XS engine web server ability, the mobile device can directly communicate to HANA appliance via HTTP protocol.  This simplifies the system architecture and extends HANA to all kinds of possibilities.

HTTP body in JSON format in WRITE API

ROOT {

  “name1” : “value1”,

  “arrayname1”: [ “arrayvalue11”, “arrayvalue12” ],

  “name2” : “value2”

  “arrayname2” : [ “arrayvalue21”, “arrayvalue22” ]

}

Parsing JSON in HANA:

var body = $.request.body.asString();

var obj = JSON.parse(body);

var name1 = obj.name1;

var name2 = obj.name2;

var arrayname1 = obj.arrayname1;

var arrayname2 = obj.arrayname2;

var i = 0

for (i; i< arrayname1.length; i++){

//write to HANA

}

HTTP body in JSON format in READ API:

{“entry”:[

                    [    

                            [“name11″,”value11”],

                             [“name21”, “value21”]

                    ],

                    [

                              [“name12″,”value12”],

                              [“name22″,”value21”]

                    ]

               ]

}

Constructing JSON in HANA:

var root = {

               entry: []

}

//rs is the return of table data

while (rs.next()){

     var jsonObj = [];

     var i = 1;

     for (i; i< columncount+1; i++){

          var record = [];

          record.push(columnlabel, rs.getString(i));

          jsonObj.push(record);

     }

     root.entry.push(jsonObj);

}

The JSON constructed from HANA this way is valid; however, it seems not necessarily represent the object array concept well.  Hope to see alternative approach.

These JSON format http body are either constructed in iPhone or consumed in iPhone for display.

To report this post you need to login first.

6 Comments

You must be Logged on to comment or reply to a post.

  1. Sascha Wenninger

    Hi Yijeng,

    I am surprised that you are showing a “manual” construction of JSON objects. Does HANA not provide a native JSON representation of query responses at least? Or is this the only way at the moment?

    Sascha

    (0) 
    1. Michael Jess

      As a side note, it is difficult to answer the question since there is no such thing as “JSON objects”.

      On the one hand, there are JavaScript objects and you do need to create those from result sets. This makes sense in that you are not likely to consume the table data as returned from the server anyway, but at least in my experience, you need to transform the information somehow.

      On the other hand, there is JSON, which you can use to serialize those objects. For that you may just use JSON.stringify() on whatever objects you created from the database contents.

      Just another comment addressing Yijeng’s criticism:

      Yijeng wrote:

                            

      The JSON constructed from HANA this way is valid; however, it seems not necessarily represent the object array concept well.  Hope to see alternative approach.

                      

      Well, you actually created an object with an array in it rather than a plain array. It serializes what you create, nothing else. This also explains why it is valid – you are not “constructing JSON”, it is generated from an object (or array) you created.

      Hope that clarifies things a bit even though I’m late to the party, at least for future Googlers. The information in this post just did not seem quite complete to me.

      Best,

      Michael

      (0) 
  2. Yijeng Liu Post author

    Alternative way to get a more common JSON object format:

    while (rs.next()){

           var numColumns = rsmd.getColumnCount();

           var jsonObj = {};

           var i = 1;

           for (i;i<numColumns+1;i++){

               var tag = rsmd.getColumnLabel(i).toLowerCase();    
         jsonObj[tag]= rs.getString(i);

        }

      root.entry.push(jsonObj);

    }

    (0) 

Leave a Reply