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 - handling null values

matt
Active Contributor
0 Likes
3,251

I have JSON data like:

[

{"NodeId":0,"Name":"ABCD","InstrumentCode":"A2545","ParameterName:"FERR","ParameterID":"PID1"},

{"NodeId":2,"Name":"BGEL","InstrumentCode":"Q436","ParameterName":"GRAPH","ParameterID":"PID3"}

]

In my transformation, I have

<tt:template>

     <array>

       <tt:loop ref=".VALUES">

         <object>

           <num name="NodeId">

             <tt:value ref="$ref.nodeid"/>

           </num>

           <str name="Name">

             <tt:value ref="$ref.name"/>

           </str>

           <str name="InstrumentCode">

             <tt:value ref="$ref.instrumentcode"/>

           </str>

           <str name="ParameterName">

             <tt:value ref="$ref.parametername"/>

           </str>

           <str name="ParameterID">

             <tt:value ref="$ref.parameterid"/>

           </str>

         </object>

       </tt:loop>

     </array>

</tt:template>

And then I deserialise the JSON data using this ABAP:

DATA: BEGIN OF value,

           nodeid         TYPE i,

           name           TYPE c LENGTH 255,

           instrumentcode TYPE c LENGTH 255,

           parametername  TYPE c LENGTH 255,

           parameterid    TYPE c LENGTH 255,

         END OF value.

DATA values LIKE STANDARD TABLE OF value WITH EMPTY KEY.

CALL TRANSFORMATION my_Transformation SOURCE XML my_json_data RESULT values = values.

The problem I have, is that within my data, I have some entries like this:

{"NodeId":12,"Name":"BGRT","InstrumentCode":"A436","ParameterName":null,"ParameterID":"PID3"}

With this, I get a ST_MATCH_FAIL dump, with the information:

The reason for the exception is:

XML comparison error

Expected element-start: "str" [ ] Read element-start: "null" [ ].

How do I construct a conditional (I guess using tt:switch and tt:d-cond) in my transformation, to handle when the ParameterName is not a string, but is null? I want to skip any null values.

1 ACCEPTED SOLUTION
Read only

matt
Active Contributor
0 Likes
1,445

Figured it out. For example:

<tt:switch>

   <tt:d-cond using="type-C($ref.parametername)">

     <str name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </str>

   </tt:d-cond>


   <tt:d-cond>

     <null name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </null>

   </tt:d-cond>


</tt:switch>

In this way, if the parametername has a value, the first condition is carried out. If it is null, the second is executed. I don't know if there is a better way, but this works!

Figured it out. For example:

<tt:switch>

   <tt:d-cond using="type-C($ref.parametername)">

     <str name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </str>

   </tt:d-cond>


   <tt:d-cond>

     <null name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </null>

   </tt:d-cond>


</tt:switch>

In this way, if the parametername has a value, the first condition is carried out. If it is null, the second is executed. I don't know if there is a better way, but this works!

2 REPLIES 2
Read only

retired_member
Product and Topic Expert
Product and Topic Expert
0 Likes
1,445

Hi Matthew,

in JSON-XML, that is the interface between JSON and ABAP, the null values of JSON are represented as separate elements

Json null → XML <null />


I guess you must react on <null /> in your trafo.


Horst


Read only

matt
Active Contributor
0 Likes
1,446

Figured it out. For example:

<tt:switch>

   <tt:d-cond using="type-C($ref.parametername)">

     <str name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </str>

   </tt:d-cond>


   <tt:d-cond>

     <null name="ParameterName">

       <tt:value ref="$ref.parametername"/>

     </null>

   </tt:d-cond>


</tt:switch>

In this way, if the parametername has a value, the first condition is carried out. If it is null, the second is executed. I don't know if there is a better way, but this works!