Skip to Content
1

Create JSON with date in Unix Time format

Jan 02, 2017 at 02:56 PM

230

avatar image

I need to create a JSON document for a REST web service call. In this JSON there is a date field that needs to formatted in Unix Time (seconds since 1st of January 1970)

Request: [
{
"ID":"XYZ",
"Date":1475272800000
} ]

The data is coming from an ABAP Structure:
ID TYPE C(8),
DATE TYPE D

Is there a way to create such a JSON directly via e.g. CALL TRANSFORMATION or is there at least a method/function module to convert ABAP dates to UNIX Time?

10 |10000 characters needed characters left characters exceeded

seconds or milliseconds? Probably the second one (AKA "ticks")

0
* Please Login or Register to Answer, Follow or Comment.

1 Answer

Best Answer
Sandra Rossi Jan 02, 2017 at 04:47 PM
1

You may create an ST transformation with <tt:value ... option="fmt(ticks)"/> (cf ST - option, Mapping Rules) which will generate "Date":/Date(1418342400000)/

If you don't want this format, then you need to use an additional ABAP field that you calculate yourself or call a method from within ST (CONVERT DATE ... INTO TIME STAMP ...)

You may generate automatically JSON from ABAP, using writer = cl_sxml_string_writer=>create( type = if_sxml=>co_xt_json ), and CALL TRANSFORMATION ... RESULT XML writer (cf SCN blog post ABAP and JSON, by Horst Keller, on 2013/01/07)

Show 6 Share
10 |10000 characters needed characters left characters exceeded

Dear Sandra,

thank you for your reply. I can't get the transformation working as described. In the documentation you linked it says option="format(ticks)" so I tried the following transformation with 4 different version of the format option.
For all versions I get a warning like "illegal value 'fmt=(ticks)'" and in the resulting JSON (created with cl_sxml_string_writer) the date is always formatted as YYYY-MM-DD.

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates">
  <tt:root name="T_DATA"/>
  <tt:template>
    <array>
      <tt:loop ref="T_DATA">
        <object>
          <str name="ID">
            <tt:value ref="$ref.id"/>
          </str>
          <num name="NumDateFmt">
            <tt:value ref="$ref.date" option="fmt=(ticks)"/>
          </num>

          <str name="StrDateFmt">
            <tt:value ref="$ref.date" option="fmt=(ticks)"/>
          </str>

          <num name="NumDate">
            <tt:value ref="$ref.date" option="format=(ticks)"/>
          </num>

          <str name="StrDate">
            <tt:value ref="$ref.date" option="format=(ticks)"/>
          </str>
        </object>
      </tt:loop>
    </array>  
</tt:template>
</tt:transform>

Kind regards!

Sebastian

0

I apologize for the missing documentation in Release 7.40.

It was delivered with Release 7.50.

New features were added in Release 7.51.

1

option="format(ticks)"

1

No it works fine. Thank you for your help!

0

Hi Sandra,

In my response I am receiving the timestamp in JSON format i.e., /Date(1462881866000)/. How do I convert this to ABAP format i.e. to YYYYMMDD format?

Thank you.

Kethan.

0

Same as already said/documented, but direction from JSON to ABAP cf Horst Keller's blog (see reference above). If you need further assistance, please open a new question.

0