Do not input private or sensitive data. View Qlik Privacy & Cookie Policy.
Skip to main content

Announcements
Learn how to migrate to Qlik Cloud Analytics™: On-Demand Briefing!
cancel
Showing results for 
Search instead for 
Did you mean: 
Nreimondo1645519314
Contributor III
Contributor III

Nested XML to JSON

Hi,

I'm new to Talend and I have this issue. I have a XML like this:

<product>

<productCode>Code1</productCode>

<productName>Name1</productName>

<Users>

<productUser>User1</productUser>

<productUser>User2</productUser>

</Users>

</product>

And i have to convert this to a JSON. Does anyone know how to achieve this?

expected JSON-array:

[{"productCode":"Code1","productName":"Name1","Users":["User1","User2"]}]

Labels (2)
9 Replies
Anonymous
Not applicable

This method may work for you.....I haven't tested it since 2018 though. So it would be interesting to see how it works for you 😉

 

https://community.talend.com/s/question/0D53p00007vCjPdCAK/a-simple-way-to-convert-an-xml-string-to-json

Nreimondo1645519314
Contributor III
Contributor III
Author

Unfortunately your link to the jar doesn't work anymore...

 

Anonymous
Not applicable

I believe this one may work. It might need a slight refactoring of the code, but I suspect it probably won't.

 

https://jar-download.com/artifacts/org.json

 

FYI if you are looking for a Jar, look at the imports and Google an import package with "jar" at the end (in this case I Googled "org.json.jsonobject jar").

Nreimondo1645519314
Contributor III
Contributor III
Author

Thanks it worked.

 

But now I need to extract the values of the productUsers.

 

I tried it with a tExtractXmlField with a loop on Users, but the result is only the first value (user1).

 

Is it unpossible because the tagnames are exactly the same and the number of users are different each product? any ideas?

Anonymous
Not applicable

Are you using the tExtractXMLField with the JSON you have just created? If so, that won't work. You need to show more detail here. Maybe a screenshot of how you have set up your tExtractXMLField.

Nreimondo1645519314
Contributor III
Contributor III
Author

No with the same XML as posted in my original question

Anonymous
Not applicable

Do you have any screenshots of your configuration of the tExtractXMLField?

Nreimondo1645519314
Contributor III
Contributor III
Author

0695b00000ODvI9AAL.png 

I tried this, but this gives me one User instead of the expected two.

Anonymous
Not applicable

Your loop needs to be.....

 

"/product/Users/productUser"

 

You will then need to tweak your other values as they will be at the level above.