Author |
Message
|
vromac |
Posted: Thu Mar 28, 2019 3:06 am Post subject: JSON double to XML decimal conversion |
|
|
Newbie
Joined: 21 Mar 2018 Posts: 7
|
Hello everyone, I have a very simple/stupid question, but can't find the proper knowledge resource to resolve it.
The context:
- using IIB v10.0.0.15
- I have a incoming JSON message that is a response from a REST service
- I want to map it to an outgoing XML message
- I am using the mapping node element
- one field on the input side is a json double type
- the destination element is a XML decimal type
The problem:
- I am using the convert transformation in the mapping node
- the source field can be NULL
[e.g, usertrace value for that field
(0x03000000:NameValue):myElement = NULL]
If I leave the convert as is, it works correctly for numeric values, however, if a NULL values comes [which is legitimate], the mapping fails with:
(0x03000000:NameValue):Text = 'Convert: element out18:myElement{ xs:decimal( xs:string( $parentElement/myElement) )}' (CHARACTER)
(0x03000000:NameValue):Text = 'IXJXE0458E: [ERR 0407][ERR FORG0001] The operand cannot be cast to type 'xs:decimal?'.' (CHARACTER)
The idea is to use the condition tab to filter NULL elements and not to convert at all. However, whichever solution I tried, it either gets false positives or false negatives
Some solutions I've used:
$myElement != null
$myElement != ''
fn:empty($myElement) = false
fn:exists($myElement) = false
fn:nillable($myElement) = false
fn:string-length($myElement) > 0
fn:boolean($myElement/text()) = fn:true()
fn:exists($myElement) and (fn:nilled( $myElement) = false)
Alternative solution - use the custom ESQL transformation, but what would be the functions signature and implementation then?
CREATE FUNCTION convertDoubleToDecimal(IN decimalAmount ???) RETURNS DECIMAL
Last edited by vromac on Mon Apr 01, 2019 6:13 am; edited 1 time in total |
|
Back to top |
|
 |
fjb_saper |
Posted: Thu Mar 28, 2019 5:09 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
Is the null value allowed for the target field? (xsd:nillable="true")?
If it is not, what value would you attribute a null value to (0)?
 _________________ MQ & Broker admin |
|
Back to top |
|
 |
vromac |
Posted: Thu Mar 28, 2019 7:44 am Post subject: |
|
|
Newbie
Joined: 21 Mar 2018 Posts: 7
|
It doesn't have the nullable=true attribute, no, but is marked optional [i.e. minOccurs=0].
So the result should be to skip the mapping and not create the destination element at all.
By the way, the solution I've been chasing now is to use custom esql with the following code
Code: |
CREATE FUNCTION convertDoubleToDecimal(IN decimalAmount FLOAT) RETURNS DECIMAL
BEGIN
DECLARE temp DECIMAL;
IF decimalAmount is not NaN Then
SET temp = CAST(decimalAmount AS DECIMAL(12,2));
else
SET temp = null;
END IF;
RETURN temp;
END;
|
Still testing for edge cases... |
|
Back to top |
|
 |
timber |
Posted: Fri Mar 29, 2019 2:16 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
Quote: |
is marked optional [i.e. minOccurs=0].
So the result should be to skip the mapping and not create the destination element at all. |
I think there is some confusion going on here. A null value is a value for an existing element. So minOccurs is not relevant (minOccurs is about whether an element exists at all).
The ESQL function is probably the best solution. |
|
Back to top |
|
 |
vromac |
Posted: Fri Mar 29, 2019 6:30 am Post subject: |
|
|
Newbie
Joined: 21 Mar 2018 Posts: 7
|
Yeah, I've figured it as a question what do I want for the destination field to look like after the call
if nillable it could look like
Quote: |
...
<someElement>blabla</someElement>
<myElement/>
<someOtherElement>blabla</someOtherElement>
... |
but, as I have it as minOccurs, I'd want it as
Code: |
...
<someElement>blabla</someElement>
<someOtherElement>blabla</someOtherElement>
... |
so yeah, the ESQL was the way I went, works as I want it for null and non-null values. |
|
Back to top |
|
 |
timber |
Posted: Fri Mar 29, 2019 7:02 am Post subject: |
|
|
 Grand Master
Joined: 25 Aug 2015 Posts: 1292
|
No, I'm afraid you have a wrong understanding of what 'nillable' means. The term 'nillable' has a very precise meaning in an XML document. See the XML specification, or a good XML tutorial. |
|
Back to top |
|
 |
vromac |
Posted: Mon Apr 01, 2019 5:20 am Post subject: |
|
|
Newbie
Joined: 21 Mar 2018 Posts: 7
|
timber wrote: |
No, I'm afraid you have a wrong understanding of what 'nillable' means. The term 'nillable' has a very precise meaning in an XML document. See the XML specification, or a good XML tutorial. |
You are right. So if the nillable attribute is used I'd get a response like
Code: |
...
<someElement>blabla</someElement>
<myElement xsi:nil='true'/>
<someOtherElement>blabla</someOtherElement>
... |
which depends on the service consumer to know how to work with it. But still, my goal was to skip the transformation and not create the destination element at all.
Seeing the final ESQL solution works, I'm content to use it and abandon the convert+conditional path. |
|
Back to top |
|
 |
|