PxPlus User Forum

Twitter Twitter Twitter

Author Topic: XML -> JSON CVS Translator  (Read 759 times)

Josh Fake

  • Member
  • **
  • Posts: 10
    • View Profile
    • Earnest & Associates
XML -> JSON CVS Translator
« on: September 25, 2019, 12:43:01 PM »
As a wish, could it be possible to add an additional option to the CVS() function or within the XML.pvc so that a custom engine to generate XML can be easily translated to JSON. 
This would allow web services that are generated in prior versions allow external systems that once requested XML change to JSON without many changes to the interfaces that generated the XML. 
 ie.  json$=cvs(xml$,"XML:JSON")
or
xml=new("*obj/XML")
........
xml$=xml'get_xml$()
json$=xml'get_json$()
if api_response$="JSON" then print(%print_fn)json$ else print(%print_fn)xml$
drop object xml

Peter.Higgins

  • Gold Member
  • ****
  • Posts: 78
    • View Profile
Re: XML -> JSON CVS Translator
« Reply #1 on: October 15, 2019, 11:51:59 AM »
Hi Josh,
Enjoyed meeting you at Direxions!

Since JSON is a data format similar to csv rather than a document format like xml, I suspect you will still have to modify the original programs for individual nodes.

I suggest making a simple object to return the JSON string when passed a node object using similar logic I use to import XML straight to data IOLISTs.

 tmp$=trn'node(n)'tag$+$24$
 VIA tmp$=trn'node(n)'value$

Mike King

  • Administrator
  • Diamond Member
  • *****
  • Posts: 3405
  • Mike King
    • View Profile
    • PVX Plus Technologies
Re: XML -> JSON CVS Translator
« Reply #2 on: October 15, 2019, 05:20:32 PM »
Josh

I have been looking into adding a JSON output method to the XML object.  There are however a few challenges as not all XML and JSON structures can be accommodated.

One example is how to handle XML attributes as opposed to elements.  There really is no logical equivalent in JSON so how should attributes be included?  One option would be to ignore them, another might be to just include them in the JSON. 

There is also a question of structure.  For example in XML you could have:

<MyData>
  <name>Mike</name>
  <address>123 main</address>
  <name>Fred Flintsone</name>
  <address>Bedrock</address>
</MyData>


In XML you can have multiple tags of the same name in the same element (think HTML which is based on XML).  You cannot have multiple elements of the same name in JSON; you can have an array but you would lose the element order.

So basically, we are looking at what we could provide, however not all XML constructs can be mapped to JSON,