Skip to Content

Convert JSON Date from external OData to YYYYMMDD in ABAP Program

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

Add a comment
10|10000 characters needed characters exceeded

Related questions

3 Answers

  • Best Answer
    Posted on Jun 26, 2020 at 04:08 PM

    Hello Bharat Bajaj

    Maybe this comment will help you: https://blogs.sap.com/2017/04/18/abap-unix-timestamp/#comment-373331

    Kind regards,
    Mateusz
    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jun 26, 2020 at 08:13 PM

    If you know the Simple Transformations/want to use one, there's a formatting option to convert the "ticks" -> See ABAP documentation - ST - Mapping Rules for Elementary Types.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Jul 17, 2020 at 04:13 AM

    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

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.