cancel
Showing results for 
Search instead for 
Did you mean: 

Remove XML duplicate key from JSON list in XML formatter

Pretpark
New Contributor II

Hello,

As you can see from the title, i am trying to remove a duplicate key in XML which is created by the XML formatter.
The JSON generator contains JSON something along the lines of this:

Pretpark_0-1727690410453.png

What i get when i run this through the XML formatter is this:

Pretpark_1-1727690449634.png

But what i would like to see is this:

Pretpark_2-1727690494309.png

I have tried every possible solution i could find (XML generator, unique snap, deduplicate snap, etc)
What i've also tried is making a Python script which converts the JSON to XML. This worked, but i could not make it work in SnapLogic's Script snap.

This is the python code:

Pretpark_3-1727690918455.png

Maybe i am overthinking it? Is it possible to use for example a Mapper snap or the XML generator to solve this issue?

Any help is appreciated.
Thanks in advance.

-Tim

1 ACCEPTED SOLUTION

Pretpark
New Contributor II

I have found the solution myself.
This is the way i did it:

Pretpark_0-1728310290837.png

The first XML formatter is the one with 'wrong' output, so i used the binary to document snap set to 'NONE' so i can map it in the mapper snap.
In the mapper snap i did the following:
'<?xml version="1.0" encoding="UTF-8"?>' + $content.substring($content.search("<Data><YOURXMLKEY>") +6).replace("</Data></YOURXMLKEY>", "").replace("</ADDRESSES><ADDRESSES>", "")

This resulted in the XML format i wanted, but it was still in 'JSON' format. So i used a CSV formatter (format to string snap in image) from which i unchecked 'write CSV header', removed the quote character, set the escape character to '\' and set the delimter to something which i know will never be in the message, in my case: '↨'.

If anyone has a better/cleaner solution, i would love to know.

-Tim

View solution in original post

2 REPLIES 2

Pretpark
New Contributor II

Edit: I dont know (or can't) change the title of the question, so i will post the edit here in the reply.
The title should be changed to: "Remove XML duplicate key from XML formatter output". I think the title causes confusion now.
Sorry

Pretpark
New Contributor II

I have found the solution myself.
This is the way i did it:

Pretpark_0-1728310290837.png

The first XML formatter is the one with 'wrong' output, so i used the binary to document snap set to 'NONE' so i can map it in the mapper snap.
In the mapper snap i did the following:
'<?xml version="1.0" encoding="UTF-8"?>' + $content.substring($content.search("<Data><YOURXMLKEY>") +6).replace("</Data></YOURXMLKEY>", "").replace("</ADDRESSES><ADDRESSES>", "")

This resulted in the XML format i wanted, but it was still in 'JSON' format. So i used a CSV formatter (format to string snap in image) from which i unchecked 'write CSV header', removed the quote character, set the escape character to '\' and set the delimter to something which i know will never be in the message, in my case: '↨'.

If anyone has a better/cleaner solution, i would love to know.

-Tim