Looking for someone to help me with XML to JSON conversion

Hello, due to lack of Javascript knowledge I am struggling to implement XML to JSON formatter, I have a project where I am receiving some of the data through api call in XML format but failing to convert into JSON. I have tried creating formatters, as documented in forums but due to the lack of js knowledge I’m not entirely sure how to call/implement xml2js conversion variable. And in general I’m not sure whether the module would work better in my case. I am receiving eBay messages through their lagacy api method, since getting them through rest api isn’t possible yet, this is the example of xml response:

<MemberMessage><MemberMessageExchange><Item><ItemID>12345678910</ItemID><ListingDetails><StartTime>2023-05-12T12:28:31.000Z</StartTime><EndTime>2023-07-12T12:28:31.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/itm/dummy-ebay-product-used-for-example</ViewItemURL></ListingDetails><Seller><UserID>dummyUser</UserID></Seller><SellingStatus><CurrentPrice currencyID=“GBP”>29.99</CurrentPrice></SellingStatus><TimeLeft>P6DT14H5M33S</TimeLeft><Title>dummy-ebay-product-used-for-example</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>None</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>make_up_sender</SenderID><SenderEmail>made_up_member@members.ebay.co.uk</SenderEmail><RecipientID>recipient_name_here</RecipientID><Subject>hello</Subject><Body>JUST LOOKING TO KNOW HOW THIS THING IS DONE</Body><MessageID>42764023432019</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-21T18:03:03.000Z</CreationDate><LastModifiedDate>2023-06-21T18:03:04.000Z</LastModifiedDate></MemberMessageExchange>

I would be willing to pay someone who could help me out with this, for either by directly guiding me through steps or writing js code for more prompt solution! Thank you!

Please checkout this library on GitHub https://github.com/factmaven/xml-to-json

I have used it once to automatically parse xml response from an API ,which provides sensor data, to JSON.

The code in the index.php file does the magic. It receives the xml data and converts it to JSON. So you have to find a way to host this file in your project as an API endpoint. You can then call that end point by passing the xml data you receive and it returns the JSON format.

If you need any help, feel free to direct message me.

1 Like

Apologies, forgot to mention I’m on Node.js server, is the process fairly similar or completely different since it’s based on js? Been looking through xml2js node package, seems fairly easy to use, just can’t get my head round it.

Hm… I think I had the same challenge two weeks ago, needed to convert XML to JSON. My case was simpler, I just needed to convert XML to JSON once, so I just used a random XML to JSON converter online. I did try making a custom module too, but I think I gave up quickly (despite not looking that difficult). Probably we both faced the same issues :man_shrugging:

The solution proposed by kkoker is the one I’d do. You can still use NodeJS, but you got to find some shared hosting for PHP to upload that PHP script. And then you use an API Action step to call that PHP script, in which you send the XML and in response you get your JSON

Oh I see! I’ll have a look at it then! Thank you! :slightly_smiling_face:

In my recent project I used xml2js package and create a custom module.
Please be advised that some XML outputs is not 100% XML format (and because of that such file processing might be different) - so try to use https://codebeautify.org/xmlvalidator or similar tools to check XML file for validation.

Hey, once stripped of unnecessary '<?xml version="1.0" encoding="UTF-8"?>\n response, my XML returns successful validation, however, once called through php endpoint, it returns an error telling me It can’t find starting ‘<’


For the matter of fact, codebeautify converts original XML to JSON without any issues, including with <?xml version="1.0" encoding="UTF-8"?>\n prefix. Now I wonder whether it has something todo with url decoding, which is unlikely, therefore it’s probably something to do from my end :thinking:

If anyone were willing to troubleshoot my dummy XML response I would be more than greatful!

`'<?xml version="1.0" encoding="UTF-8"?>\n<GetMemberMessagesResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2023-07-06T10:32:56.196Z</Timestamp><Ack>Success</Ack><Version>1177</Version><Build>E1177_CORE_APIMSG_19110890_R1</Build><MemberMessage><MemberMessageExchange><Item><ItemID>ItemId</ItemID><ListingDetails><StartTime>2023-06-10T10:57:15.000Z</StartTime><EndTime>2023-06-14T16:33:25.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">229.99</CurrentPrice></SellingStatus><Title>FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015 #115829021064</Subject><Body>Hi will this fit my car my reg is ...</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:57:28.000Z</CreationDate><LastModifiedDate>2023-06-12T05:22:11.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-03-21T21:54:12.000Z</StartTime><EndTime>2023-07-02T21:45:29.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">53.99</CurrentPrice></SellingStatus><Title>MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800 #115738891031</Subject><Body>PK17 USB</Body><MessageID>messageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:18:10.000Z</CreationDate><LastModifiedDate>2023-06-12T06:02:05.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-05-15T18:16:07.000Z</StartTime><EndTime>2023-07-15T18:16:07.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/itm</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">14.99</CurrentPrice></SellingStatus><TimeLeft>P9DT7H43M11S</TimeLeft><Title>BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067 #115805443272</Subject><Body>hello\nis that tweeter covers?\nwould they fit F15?\n\nthanks</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T19:07:34.000Z</CreationDate><LastModifiedDate>2023-06-11T19:07:34.000Z</LastModifiedDate></MemberMessageExchange></MemberMessage><PaginationResult><TotalNumberOfPages>2</TotalNumberOfPages><TotalNumberOfEntries>6</TotalNumberOfEntries></PaginationResult><HasMoreItems>true</HasMoreItems></GetMemberMessagesResponse>'`

Very easy problem

Are you sure it’s starting with <?

Hint: It’s starting with ', look at the URL being called, starts with %27, go search ASCII/URL encoding table and see what %27 belongs to

Also, that “unnecessary” thing you removed, I’m not sure why you felt the need to do that :stuck_out_tongue:

<?xml version="1.0" encoding="UTF-8"?>\n<GetMemberMessagesResponse

delete \n after <?xml version="1.0" encoding="UTF-8"?> and try again please

2 Likes

Apologies, should have said that removing ' haven’t changed much, for some reason returning the same exact error.

Deleting \n unfortunately presents premature ending error. :thinking:

I have hosted script on the development domain, feel free to call it and check where I could posibble be making a mistake in the call :sob:

`http://busy-elion.217-174-244-244.plesk.page/index.php?xml=<?xml version="1.0" encoding="UTF-8"?>\n<GetMemberMessagesResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2023-07-06T10:32:56.196Z</Timestamp><Ack>Success</Ack><Version>1177</Version><Build>E1177_CORE_APIMSG_19110890_R1</Build><MemberMessage><MemberMessageExchange><Item><ItemID>ItemId</ItemID><ListingDetails><StartTime>2023-06-10T10:57:15.000Z</StartTime><EndTime>2023-06-14T16:33:25.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">229.99</CurrentPrice></SellingStatus><Title>FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015 #115829021064</Subject><Body>Hi will this fit my car my reg is ...</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:57:28.000Z</CreationDate><LastModifiedDate>2023-06-12T05:22:11.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-03-21T21:54:12.000Z</StartTime><EndTime>2023-07-02T21:45:29.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">53.99</CurrentPrice></SellingStatus><Title>MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800 #115738891031</Subject><Body>PK17 USB</Body><MessageID>messageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:18:10.000Z</CreationDate><LastModifiedDate>2023-06-12T06:02:05.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-05-15T18:16:07.000Z</StartTime><EndTime>2023-07-15T18:16:07.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/itm</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">14.99</CurrentPrice></SellingStatus><TimeLeft>P9DT7H43M11S</TimeLeft><Title>BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067 #115805443272</Subject><Body>hello\nis that tweeter covers?\nwould they fit F15?\n\nthanks</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T19:07:34.000Z</CreationDate><LastModifiedDate>2023-06-11T19:07:34.000Z</LastModifiedDate></MemberMessageExchange></MemberMessage><PaginationResult><TotalNumberOfPages>2</TotalNumberOfPages><TotalNumberOfEntries>6</TotalNumberOfEntries></PaginationResult><HasMoreItems>true</HasMoreItems></GetMemberMessagesResponse>`
1 Like

Your response XML is 100% valid - I’ve checked with https://codebeautify.org/xmltojson

Yea, that’s what I’ve figured, odd, do you think there could be an issue in function instead? As if you would load my previously mentioned url with xml output, it returns an error instead… :thinking: I guess at least I know the XML values I’m working with are valid, I’ll have to keep looking for a working solution then whether it is locally in node or through api call

You can also try the JavaScript or NodeJS code I have added on GitHub https://github.com/kvskoker/xml-to-json

Clone the Wappler NodeJS project and try it out.
You will require to install xmldom.
npm install xmldom

See the example API in the project. Hope this helps.

Instead of $_GET use $_POST as it’s more suitable for large data

Edit: My bad, the PHP script only accepts $_GET

This solution is correct, thanks @famousmag!

I checked with your live script @Evaldas, you need to delete all occurrences of \n

Fixed XML for convenience:

<?xml version="1.0" encoding="UTF-8"?><GetMemberMessagesResponse xmlns="urn:ebay:apis:eBLBaseComponents"><Timestamp>2023-07-06T10:32:56.196Z</Timestamp><Ack>Success</Ack><Version>1177</Version><Build>E1177_CORE_APIMSG_19110890_R1</Build><MemberMessage><MemberMessageExchange><Item><ItemID>ItemId</ItemID><ListingDetails><StartTime>2023-06-10T10:57:15.000Z</StartTime><EndTime>2023-06-14T16:33:25.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">229.99</CurrentPrice></SellingStatus><Title>FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about FORD KUGA MK2 2.0 DIESEL RADIATOR PACK RAD PACK 2015 #115829021064</Subject><Body>Hi will this fit my car my reg is ...</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:57:28.000Z</CreationDate><LastModifiedDate>2023-06-12T05:22:11.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-03-21T21:54:12.000Z</StartTime><EndTime>2023-07-02T21:45:29.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">53.99</CurrentPrice></SellingStatus><Title>MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about MITSUBISHI ASX 2012 FRONT RIGHT DRIVER SIDE SEAT BELT 604675800 #115738891031</Subject><Body>PK17 USB</Body><MessageID>messageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T22:18:10.000Z</CreationDate><LastModifiedDate>2023-06-12T06:02:05.000Z</LastModifiedDate></MemberMessageExchange><MemberMessageExchange><Item><ItemID>Itemid</ItemID><ListingDetails><StartTime>2023-05-15T18:16:07.000Z</StartTime><EndTime>2023-07-15T18:16:07.000Z</EndTime><ViewItemURL>https://www.ebay.co.uk/itm</ViewItemURL></ListingDetails><Seller><UserID>company</UserID></Seller><SellingStatus><CurrentPrice currencyID="GBP">14.99</CurrentPrice></SellingStatus><TimeLeft>P9DT7H43M11S</TimeLeft><Title>BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067</Title><ConditionID>3000</ConditionID><ConditionDisplayName>Used</ConditionDisplayName></Item><Question><MessageType>AskSellerQuestion</MessageType><QuestionType>General</QuestionType><DisplayToPublic>false</DisplayToPublic><SenderID>SenderId</SenderID><SenderEmail>SenderEmail</SenderEmail><RecipientID>company</RecipientID><Subject>user sent a message about BMW 3 SERIES F30 2012 DOOR SPEAKER TRIM COVER GRILL 1 PSC 7260067 #115805443272</Subject><Body>hellois that tweeter covers?would they fit F15?thanks</Body><MessageID>MessageId</MessageID></Question><MessageStatus>Unanswered</MessageStatus><CreationDate>2023-06-11T19:07:34.000Z</CreationDate><LastModifiedDate>2023-06-11T19:07:34.000Z</LastModifiedDate></MemberMessageExchange></MemberMessage><PaginationResult><TotalNumberOfPages>2</TotalNumberOfPages><TotalNumberOfEntries>6</TotalNumberOfEntries></PaginationResult><HasMoreItems>true</HasMoreItems></GetMemberMessagesResponse>

For automated fixing, use a Set Value with a replace formatter (find \n, replace with empty string; the UI might not allow you to set an empty string, you might need to fix in the expression directly)

Program used for testing: Insomnia

2 Likes

Bravo! Thank you so much @Apple , @kkoker & @famousmag!
Actually both of the recently shown solutions are working working very well! I appreciate all the time and effort you have put, thanks to you I am able to carry on with my project :wink: :confetti_ball:

3 Likes