cancel
Showing results for 
Search instead for 
Did you mean: 

How to handle Dates & Numbers before saving JSON Model to OData Model/backend?

0 Kudos

I generate a JSON Model, which gets saved to the SAP backend (an InfoObject) using an OData Model.

Unfortunately, when the JSON Model contains DateTimes or percentages, currencies etc., that doesn't work properly. For the dates, it gets saved as "/Date..." and for percentages etc. I get a "parsing error"

That's how I e.g. save new data to the backend (working fine for any strings!):

//some other code before..
insert = _.map(insert, function(data) { return _.omit(data, "highlight"); });
	_.forEach(insert, data => {
		var entry = (modelprefix + "('" + data.COL01 + "')");
		oModel.create(modelprefix, data, mParameters);
	});
oModel.submitChanges();

Here's an example of how the insert Model could look like:

The first object contains a Date object in COL02, which gets displayed like that after the saving: /Date(1313186400000) (read about this, that this is the JSON Model date formatting)

and for the second object, I get an error ("ERROR while parsing an XML-stream") since COL02 contains a percentage.

Could I loop through every object before saving and format/parse it properly? If yes, how can I achieve this? Or is there another way?

Accepted Solutions (0)

Answers (1)

Answers (1)

SergioG_TX
Active Contributor
0 Kudos

for the datetime object... this is because that is how the JSON representation is displayed... you should get the number inside the parenthesis... and in your JS do... new Date(NUMBER_INSIDE_PARENTHESIS) to get the right date. as far as %s.... i dont see the % sign.. but most likely you need to match the target datatype... im curious to know why your COL02 has a date on the first object and then an integer on the second one... this may be the reason of the parsing error ...