cancel
Showing results for 
Search instead for 
Did you mean: 

Convert JSON Date from external OData to YYYYMMDD in ABAP Program

bharatbajaj
Active Participant
8,519

Hi SAP Experts,

I am calling an external OData Service in my SAP ECC System (Netweaver version 7.0) using CL_HTTP_CLIENT.

When I call the service, I get the Date in JSON format, which the Unix/Epoch Format : “\/Date(1567981296000)\/”

As per the Unix format this value means 01.01.1970 + 1567981296000 seconds.

I want to convert this date into a regular SAP date-time format (YYYYMMDD HH:MM:SS) in my ABAP Code.

However, I'm not able to find any readymade FM which can do this conversion and give me the real date.

Can someone please provide the solution for this?

Regards,

Bharat Bajaj

View Entire Topic
bharatbajaj
Active Participant

Now I found a better way to handle the JSON DateTime value.

If I declare my ABAP field as:

LV_DATETIME TYPE TIMESTAMP, (instead of string)

the /ui2/cl_json class will automatically transform the JSON Date into abap Timestamp.

try this sample code :

TYPES : BEGIN OF lty_data,
          order_no     TYPE char10,
          create_date  TYPE timestamp,  " <<<<<<<<< This is the Trick
        END OF lty_data.

DATA: ls_data  TYPE lty_data,
      lv_json  TYPE /ui2/cl_json=>json.

lv_json = '{ "Order_No" : "0021232324" , "Create_Date" : "/Date(1596240000000)/" }'.

/ui2/cl_json=>deserialize(
  EXPORTING
    json        = lv_json             " JSON string
  CHANGING
    data        = ls_data ).          " Converted Data

After deserialize, you'll notice that ls_data-create_date is converted to 20200801000000.

Such a simple thing, yet extremely important.

Hope this helps.

Regards,

Bharat Bajaj

ricky_shaw
Contributor
0 Kudos
Hi, I tried this but i am seeing 0 for ls_data-create_date..can you help?