XML data in table

Hello,

I am using a simple GET query that returns XML and I guess I need to convert that to json to put it in a table but when I do the converting it looks like this in the table...

sample XML looks like this...

<order_list>
<order>
<client_code>WIT0909</client_code>
<ship_req_id>23-010619921</ship_req_id>
<process_status>HOLD</process_status>
<ordered_date>01/06/2023</ordered_date>
<po_number></po_number>
<client_ref_id>M080586233</client_ref_id>
<comp_name></comp_name>
<full_name></full_name>
<addr1_line>E</addr1_line>
<addr2_line></addr2_line>
<city_name>NEW YORK</city_name>
<state_code>NY</state_code>
<zip_code>10019-6002</zip_code>
<cntry_code>US</cntry_code>
<phone_no></phone_no>
<email_id></email_id>
<editing_id></editing_id>
</order>
<order>
<client_code>WIT0909</client_code>
<ship_req_id>23-010621504</ship_req_id>
<process_status>HOLD</process_status>
<ordered_date>01/06/2023</ordered_date>
<po_number></po_number>
<client_ref_id>M080586563</client_ref_id>
<comp_name></comp_name>
<full_name></full_name>
<addr1_line></addr1_line>
<addr2_line></addr2_line>
<city_name></city_name>
<state_code>CO</state_code>
<zip_code>80915-3675</zip_code>
<cntry_code>US</cntry_code>
<phone_no></phone_no>
<email_id></email_id>
<editing_id></editing_id>
</order>
</order_list>

Hi Juspitt - welcome to the forum!

You'll most likely need to use a third-party library such as xml-js.
In your app settings, add a library as such;

Library to add:
https://unpkg.com/xml-js@1.6.11/dist/xml-js.min.js

Documentation:

Hope this helps!
Jonathan

Hi Jonathan,

Yes I have already installed that and that's why the code in the Transforer is for its converting the XML to JSON but just in a wield way and its making an JSON array foreach element name which I dont think is correct.

EDIT

getting slightly closer to what I need but still the same issue for some reason its putting its in own "_text" element

Given that it looks like your XML structure is flat, you could just return a mapped version, such as;

return _.map(xmlResults, e => e._text);

(in your transformer on line 4, replace your code with this)


However, since XML can contain attributes, those will get lost if you XML input changes.
I.e.

<amount sign="plus">200</amount>

would return

{amount: 200}

and you'd loose the context that it's positive in this case.

Thanks for sharing this solution, @jonathanbredo!

@juspitt, as far as displaying the data in the table component, you could also use a mapper to key into `_.text` though handling it in JS is likely faster & easier for referencing the data in other components :slightly_smiling_face: