2016 Aug 25 12:56 PM
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.
2016 Aug 26 9:27 AM
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!
2016 Aug 25 1:28 PM
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
2016 Aug 26 9:27 AM
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!
| User | Count |
|---|---|
| 3 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 | |
| 1 |