READ & WRITE JSON API from HANA
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.
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
If you mean OData, yes. However, this is focus on using javascript for constructing and processing.
Please check this blog as well. Thank you.
http://scn.sap.com/community/developer-center/hana/blog/2013/05/30/hana-xsengine-experienced-for-mobile-application
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:
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
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);
}
I need your help in writing an API(XSJS service) which will take JSON format data and inserted into HANA database.