Application Development and Automation Discussions
Join the discussions or start your own on all things application development, including tools and APIs, programming models, and keeping your skills sharp.
cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

Simple Transformation: Map XML boolean to ABAP boolean

niall_brennan4
Explorer
2,357

I am using ST for the deserialization of JSON files (yes I know about /ui2/cl_json=>deserialize() but that does not meet my requirements).

I just want to be able to check that a value is true and if so then map the correct abap boolean value (X). The problem is with returning the correct value.

My Sample Program

REPORT z_json_abap2.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    TYPES: BEGIN OF ty_s_line,
             posnr  TYPE posnr,
             return TYPE boole_d,
             reason TYPE char3,
           END OF ty_s_line.

    TYPES: BEGIN OF ty_s_header,
             order_id TYPE vbeln,
             items    TYPE TABLE OF ty_s_line WITH DEFAULT KEY,
           END OF ty_s_header.

    DATA: lt_order TYPE ty_s_header.


    DATA(lv_json) = cl_abap_codepage=>convert_to(
   `{` &&
  ` "order_id": "51324", ` &&
  ` "items": [ ` &&
  ` { ` &&
  `   "line": "01", ` &&
  `   "return": true, ` &&
  `   "order_reason": "ABC" ` &&
  `   }, ` &&
  `   { ` &&
  `   "line": "02", ` &&
  `   "return": true ` &&
  `   }, ` &&
  `   { ` &&
  `   "line": "03", ` &&
  `   "return": false, ` &&
  `   "order_reason": null ` &&
  `   } ` &&
  `  ] ` &&
  `  } `  ).


    CALL TRANSFORMATION zst_order_test SOURCE XML lv_json RESULT root = lt_order.
  ENDMETHOD.
ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

I check that the element has the value "true" - and if it does I want to return the appropriate value for an ABAP_BOOLEAN . I have tried many options. It this latest version it raises an exception that the value "X" was expected.

<?sap.transform simple?>
<tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
  <tt:root name="ROOT" type="?"/>
  <tt:variable name="RETURN"/>
  <tt:variable length="1" name="ABAP_TRUE" type="C" val="'X'"/>
  <tt:template>
    <object>
      <str name="order_id">
        <tt:value option="noError" ref="ROOT.order_id"/>
      </str>
      <array>
        <tt:loop ref="ROOT.items">
          <object>
            <str name="line">
              <tt:value ref="$ref.posnr"/>
            </str>
            <bool name="return">
              <tt:read type="C" var="RETURN"/>
              <tt:cond-var check="RETURN='true'">'X'</tt:cond-var>
              <tt:cond-var check="RETURN!='true'"/>
              <tt:skip/>
            </bool>
            <tt:cond>
              <str name="order_reason">
                <tt:value option="noError" ref="$ref.reason"/>
              </str>
            </tt:cond>
            <tt:cond>
              <null name="order_reason">
                <tt:value option="noError" ref="$ref.reason"/>
              </null>
            </tt:cond>
          </object>
        </tt:loop>
      </array>
    </object>
  </tt:template>
</tt:transform>
2 REPLIES 2
Read only

Sandra_Rossi
Active Contributor
Read only

niall_brennan4
Explorer
1,676

Thanks to a tip from Sandra this is now solved. For those who are interested here is the solution (the key point being to use XSDBOOLEAN - which I thought I had tried but apparently not in the right combination with everything else).

REPORT z_json_abap2.

CLASS demo DEFINITION.
  PUBLIC SECTION.
    CLASS-METHODS main.
ENDCLASS.

CLASS demo IMPLEMENTATION.
  METHOD main.

    TYPES: BEGIN OF ty_s_line,
             posnr  TYPE posnr,
             return TYPE xsdboolean,
             reason TYPE char3,
           END OF ty_s_line.

    TYPES: BEGIN OF ty_s_header,
             order_id TYPE vbeln,
             items    TYPE TABLE OF ty_s_line WITH DEFAULT KEY,
           END OF ty_s_header.

    DATA: lt_order TYPE ty_s_header.


    DATA(lv_json) = cl_abap_codepage=>convert_to(
   `{` &&
  ` "order_id": "51324", ` &&
  ` "items": [ ` &&
  ` { ` &&
  `   "line": "01", ` &&
  `   "return": true, ` &&
  `   "order_reason": "ABC" ` &&
  `   }, ` &&
  `   { ` &&
  `   "line": "02", ` &&
  `   "return": true ` &&
  `   }, ` &&
  `   { ` &&
  `   "line": "03", ` &&
  `   "return": false, ` &&
  `   "order_reason": null ` &&
  `   } ` &&
  `  ] ` &&
  `  } `  ).

    TRY.
        CALL TRANSFORMATION zst_order_test SOURCE XML lv_json RESULT root = lt_order.
      CATCH cx_transformation_error INTO DATA(exc).
        cl_demo_output=>display( exc->get_text( ) ).
        RETURN.
    ENDTRY.

  ENDMETHOD.

ENDCLASS.

START-OF-SELECTION.
  demo=>main( ).

Working Transformation

    <?sap.transform simple?>
    <tt:transform xmlns:tt="http://www.sap.com/transformation-templates" xmlns:ddic="http://www.sap.com/abapxml/types/dictionary" xmlns:def="http://www.sap.com/abapxml/types/defined">
      <tt:root name="ROOT" type="?"/>
      <tt:template>
        <object>
          <str name="order_id">
            <tt:value option="noError" ref="ROOT.order_id"/>
          </str>
          <array>
            <tt:loop ref="ROOT.items">
              <object>
                <str name="line">
                  <tt:value ref="$ref.posnr"/>
                </str>
                <bool name="return">
                    <tt:value option="noError" ref="$ref.return"/>
                </bool>
                <tt:cond>
                  <str name="order_reason">
                    <tt:value option="noError" ref="$ref.reason"/>
                  </str>
                </tt:cond>
                <tt:cond>
                  <null name="order_reason">
                    <tt:value option="noError" ref="$ref.reason"/>
                  </null>
                </tt:cond>
              </object>
            </tt:loop>
          </array>
        </object>
      </tt:template>
    </tt:transform>