Convert contents of memptr containing XML into JSON

Posted by onnodehaan on 30-Nov-2018 15:52

Hi guys

I have bit of a puzzle. :-)

In an old part of our system we are saving XML-files to a temp-table as raw data. We can get it out, by copying the rawdata into a memptr.

This memptr contains an XML. How can I easily convert this memptr containing XML into JSON in an elegant fashion?

Hopefully you guys can help me out a bit.

All Replies

Posted by Matt Baker on 30-Nov-2018 15:55

Deleted...didn't read the problem correctly.

Posted by goo on 30-Nov-2018 16:08

You will probably have to read the xml into a temp-table via dataset, then writing it to json.


Def var dsh as handle no-undo.
Def var lcjson as longchar no-undo.

Create dataset dsh.
Dsh:read-xml(‘mempntr’,mymptr).
Dsh:get-buffer-handle(1):write-json(‘longchar’,lcjson).

Or Something like it


30. nov. 2018 kl. 16:53 skrev onnodehaan <bounce-onnodehaan@community.progress.com>:

Update from Progress Community
onnodehaan

Hi guys

I have bit of a puzzle. :-)

In an old part of our system we are saving XML-files to a temp-table as raw data. We can get it out, by copying the rawdata into a memptr.

This memptr contains an XML. How can I easily convert this memptr containing XML into JSON in an elegant fashion?

Hopefully you guys can help me out a bit.

View online

 

You received this notification because you subscribed to the forum.  To stop receiving updates from only this thread, go here.

Flag this post as spam/abuse.

Posted by frank.meulblok on 30-Nov-2018 16:14

If the XML structure allows reading into a dataset/temp-table via READ-XML, you could then use WRITE-JSON to write it out again.

Otherwise, use the SAX-READER to parse the XML, and from it's callbacks use the Progress.Json.* classes to build the JSON string.

As first, generic approach you'll probably want map each XML element to a new JSON object, and add XML attributes as attributes of that JSON object. Map XML sub-elements to a JSON array of JSON objects.

If you know what JSON layout you want to use for a given XML document type, change that approach as desired.

(You could also use the X-DOCUMENT DOM tree, but well, for this purpose the SAX approach tends to be more elegant and lightweight.)

Posted by onnodehaan on 30-Nov-2018 18:02

Hi guys

Yes both options came to mind with me as well. Only  "problem" that I have is that it requires a lot of code to use sax-reader for example. And construct the JSON manually.

But unless someone comes up with a fast, shorter, way: I guess using sax-reader is indeed the least worst option :-)

Posted by goo on 30-Nov-2018 18:24

Are you saying that you ain’t able to do the read-xml into dataset?
 
//Geir Otto
 

Posted by onnodehaan on 30-Nov-2018 18:59

In some cases I might not. I don't know the type of XML beforehand.

Posted by goo on 30-Nov-2018 19:10

So there are some of the xml’s that will not be read using read-xml ? but some will? The I would definitely do those that will &#128522; remember that the xml is read into dataset as many temp-tables. We have used this method with great result.
 
Good luck !
 
//Geir Otto
 

Posted by vjug on 30-Nov-2018 20:57

One aproach I would suggest (if you are comfortable using .NET controls) is running XSL transformation that converts XML to JSON. Maybe this link might help: stackoverflow.com/.../xsl-to-convert-xml-to-json

This thread is closed