Author |
Message
|
saviobarr |
Posted: Tue Mar 14, 2017 5:17 am Post subject: JSon to XML with xsi:nil="true" |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
Hi everyone,
I have a requirement where a consumer sends a JSon and some fields have a null value, and I have to parse it to a XML, creating a xsi:nil="true" atribute (I know, it should not be sent neither present in message tree).
Current scenario:
Input json:
Transformation using ESQL:
Code: |
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.name;
|
It produces a XML like below:
Code: |
<Root>
<name/>
</Root>
|
The problem is: the validation is enabled and the output, when the value in the JSon is null, must be like similar to the following:
Code: |
<Root>
<name xsi:nil="true" />
</Root> |
I did research the Knowledge Center, and it says:
"Typically, the XML parsers (XMLNSC, XMLNS, and XML) do not create null values in the message tree; an empty element or an empty attribute value merely produces an empty string value in the message tree."
In short: if some property in the incoming JSon has a null value, the generated XML atribute must contain xsi:nil="true" atribute.
Any ideas?
Many thanks
Savio Barros _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
smdavies99 |
Posted: Tue Mar 14, 2017 7:23 am Post subject: |
|
|
 Jedi Council
Joined: 10 Feb 2003 Posts: 6076 Location: Somewhere over the Rainbow this side of Never-never land.
|
This question has to my memory been asked before.
In effect you need to know how to create an XML formatted output that contains fields that are present (with tags) but have no value.
using the search feature of this forum (upper right of the screen) may well help you find rhe answer. _________________ WMQ User since 1999
MQSI/WBI/WMB/'Thingy' User since 2002
Linux user since 1995
Every time you reinvent the wheel the more square it gets (anon). If in doubt think and investigate before you ask silly questions. |
|
Back to top |
|
 |
adubya |
Posted: Tue Mar 14, 2017 7:59 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
|
Back to top |
|
 |
saviobarr |
Posted: Tue Mar 14, 2017 8:22 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
Hi,
Thanks for replying. I saw that KC page before posting here. It does not clarify if using ESQL mapping there is a way to tell the message to create a xsi:nil="true" if the incoming field is null. For now, I am getting by testing if the incoming field is null and deciding if it should be mapped or set a xml field with xsi:nil = 'true';
Code: |
IF InputRoot.JSON.Data.name IS NOT NULL THEN
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.name;
ELSE
SET OutputRoot.XMLNSC.Root.name.(XMLNSC.Attribute)xsi:nil = 'true';
END IF;
|
It works, but seems ugly. Any better suggestion?
Thanks _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
mqjeff |
Posted: Tue Mar 14, 2017 8:30 am Post subject: |
|
|
Grand Master
Joined: 25 Jun 2008 Posts: 17447
|
Code: |
SET OutputRoot.XMLNSC.Root.name.(XMLNSC.Attribute)xsi:nil = 'true';
IF InputRoot.JSON.Data.name IS NOT NULL THEN
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.name;
END IF; |
 _________________ chmod -R ugo-wx / |
|
Back to top |
|
 |
saviobarr |
Posted: Tue Mar 14, 2017 8:43 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
smdavies99 wrote: |
In effect you need to know how to create an XML formatted output that contains fields that are present (with tags) but have no value. |
Thanks for replying. I actually need to know if ESQL supports implicitly xsi:nil="true" in a destination field, if the source field is null. If am testing each single incoming field before mapping it, to decide either map or explicitly create a output with a xsi:nil="true" attribute:
Code: |
IF InputRoot.JSON.Data.Root.name IS NOT NULL THEN
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.Root.name;
ELSE
SET OutputRoot.XMLNSC.Root.name.(XMLNSC.Attribute)xsi:nil = 'true';
END IF;
|
It does not look the best solution... do you have any suggestion?
Quote: |
using the search feature of this forum (upper right of the screen) may well help you find rhe answer. |
Thanks for your advice. Researching before asking can avoid waste of time, providing information that can be looked up. I searched before, and I couldn't find any post that fits my doubt. _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
saviobarr |
Posted: Tue Mar 14, 2017 8:45 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
mqjeff wrote: |
Code: |
SET OutputRoot.XMLNSC.Root.name.(XMLNSC.Attribute)xsi:nil = 'true';
IF InputRoot.JSON.Data.name IS NOT NULL THEN
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.name;
END IF; |
 |
Looks better. Thanks. _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
fjb_saper |
Posted: Wed Mar 15, 2017 5:36 am Post subject: |
|
|
 Grand High Poobah
Joined: 18 Nov 2003 Posts: 20756 Location: LI,NY
|
There is something much simpler.
Just build the tree.
Then write a procedure that checks each element and if null adds the attribute xsi:nil=true. Remember you will have to run the procedure recursively...
And make it reusable for any other XML Message Tree.
Have fun  _________________ MQ & Broker admin |
|
Back to top |
|
 |
saviobarr |
Posted: Wed Mar 15, 2017 6:01 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
fjb_saper wrote: |
There is something much simpler.
Just build the tree.
Then write a procedure that checks each element and if null adds the attribute xsi:nil=true. Remember you will have to run the procedure recursively...
And make it reusable for any other XML Message Tree.
Have fun  |
Hi, thanks for the suggestion.
When I map a JSon null field to XML field, the output does not create a XML field, plus, the JSon and XML have their message structure (field path and field names) very different. If I traverse the entire XML after mapping, it's not possible to identify if empty tags should or shouldn't have the attribute nil. I did map each field, testing if the JSon input is null and this way worked. But I am still wondering if there is a better solution.
Thanks
Savio Barros _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
adubya |
Posted: Wed Mar 15, 2017 6:37 am Post subject: |
|
|
Partisan
Joined: 25 Aug 2011 Posts: 377 Location: GU12, UK
|
Aways create the XML elements and then only set their content if the original JSON input is not null. You could then perform the tree walking and setting of "empty" elements to have the nill attribute.
i.e.
Code: |
CREATE FIELD outRef.elementName;
IF inRef.inputFieldName IS NOT NULL THEN
SET outRef.elementName VALUE = inRef.inputFieldName
END IF;
|
then perform the tree walk after your output structure is complete.
I'd probably create a utility PROCEDURE which performed the above code + had an ELSE clause to set the nill attribute if the input data was null/empty.
Then call the procedure for each element you're mapping _________________ Independent Middleware Consultant
andy@knownentity.com |
|
Back to top |
|
 |
saviobarr |
Posted: Wed Mar 15, 2017 6:46 am Post subject: |
|
|
Centurion
Joined: 21 Oct 2014 Posts: 100 Location: Sao Paulo, Brazil
|
adubya wrote: |
Aways create the XML elements and then only set their content if the original JSON input is not null. You could then perform the tree walking and setting of "empty" elements to have the nill attribute.
i.e.
Code: |
CREATE FIELD outRef.elementName;
IF inRef.inputFieldName IS NOT NULL THEN
SET outRef.elementName VALUE = inRef.inputFieldName
END IF;
|
then perform the tree walk after your output structure is complete.
I'd probably create a utility PROCEDURE which performed the above code + had an ELSE clause to set the nill attribute if the input data was null/empty.
Then call the procedure for each element you're mapping |
Thanks for replying.
I did this:
Code: |
SET OutputRoot.XMLNSC.Root.name.(XMLNSC.Attribute)xsi:nil = 'true';
IF InputRoot.JSON.Data.name IS NOT NULL THEN
SET OutputRoot.XMLNSC.Root.name = InputRoot.JSON.Data.name;
END IF; |
The result is the same  _________________ Go as far as you can go. Then go farther! |
|
Back to top |
|
 |
|