Question
· Sep 17

Need help with XML to JSON format

Hello to my fellow Cache Gurus:

I ran into two issues with Cache to XML Export and Cache to JSON Export in regard to array sequences.   So before I waste time opening a WRC ticket, I figured I would poll the Development Community, since there is always so much wonderful feedback and suggestions via this Developer Community!   So much thanks in advance for everyone's input!   Go Team!

I have multiple classes that extend from %RegisteredObject and %XML.Adaptor.  These classes are populated with data from our application and the data of the overall class structure is exported as XML (and soon to be JSON) format.   And the XML export (and even JSON export) works very well, except for one small issue.

For a couple of the classes, there is a property that is defined as an array of another class.   Example:    Property Slides As array Of Q.Interface.Fragment.IMS.Slide;

When the objects in the array get exported to XML they show up with an attribute such as this:

          <slide slideSeq="1">    
                    // other property values show here
          </slide>
          <slide slideSeq="2">
                    // other property values show here
          </slide>

How can I get rid of that attribute?    Or is there a way to instead have the attribute export as an element?

When I try to output the data as JSON it is even worse.   The attribute name is tossed away as well as the object name (slide) and only the sequence number value is included.

Here is an example of the JSON:

                "1": {
                    // other property values show here
                },
                "2": {
                    // other property values show here
                },

I have no clue how to get the JSON to export properly with maintaining the object name (slide) like shown for the XML and suppressing the sequence value.

Thank you all!

Product version: Caché 2017.1
Discussion (6)2
Log in or sign up to continue

Wow!  Thank you for the timely reply.   I will go and try that right now.   If that works, then you will be my hero!   I wasted several hours over the past few days trying a bunch of different things and looking for some secret hidden feature such as a keyword like XMLKEYNAMEASELEMENT = 1 or something like that where the built-in Cache code might have allowed XML attributes to be instead exported as an element (which would have been a really cool hidden feature if it existed).   But I will definitely try your suggestion today.

This is why I like the InterSystems Developer Community so much.  There are folks like you who have many years of knowledge and experience, as well as, those who have run into similiar issues, so all of you always have so many wonderful ideas, thoughts, and suggestions to help fellow Cache programmers.

Thanks again for your time and advice.   Go Team!!!!

Following your enthusiasm I implemented a little test 😁

Class Community.Array.Base Extends (%RegisteredObject, %XML.Adaptor, %JSON.Adaptor)
{

Property Slide As list Of Community.Array.Slide(XMLPROJECTION = "ELEMENT");

ClassMethod test()
{
	Set Base=##class(Community.Array.Base).%New()
	For i=1:1:3 {
		Set Slide=##class(Community.Array.Slide).%New()
		Set Slide.Name="Name"_i
		Do Base.Slide.Insert(Slide)
	}
	Do Base.XMLExport(,",indent")
	Write !,"JSON: ",!
	Do Base.%JSONExport()
}

}

Class Community.Array.Slide Extends (%RegisteredObject, %XML.Adaptor, %JSON.Adaptor)
{

Property Name As %String;

}

The output is:

USER>Do ##class(Community.Array.Base).test()
<Base>
  <Slide>
    <Name>Name1</Name>
  </Slide>
  <Slide>
    <Name>Name2</Name>
  </Slide>
  <Slide>
    <Name>Name3</Name>
  </Slide>
</Base>
 
JSON:
{"Slide":[{"Name":"Name1"},{"Name":"Name2"},{"Name":"Name3"}]}

Is this what you need?

Exactly!    I updated my code based on your suggestion and it worked like a charm.   It looks just like what you demonstrated above.    I really appreciate the information you provided.

Side Note:   The one thing that I noticed (that was a little annoying from the Cache perspective) is my XML Export uses the XMLNAME keyword values, but the JSON Export does not.

Here is an example of my real property names from the Slide class with the XMLNAME keyword values:

Property SlideSequence As %Integer(XMLNAME = "slideSeq");
Property BarcodeText As %String(MAXLEN = 100, XMLNAME = "barcodeID");
Property StainType As %String(MAXLEN = 200, XMLNAME = "stainType");
Property StainTypeMapped As %String(MAXLEN = 200, XMLNAME = "mappedStainType");
Property Block As %String(XMLNAME = "blockNumber");

The XML Export uses the XMLNAME value.   But the JSON Export uses the class property name.   I looked in the documentation to see if there was a JSONNAME keyword - but no luck.   So that is a little nuance and inconsistency with Cache.  I like how we can encapsulate the real property name with an alias using XMLNAME (we use the XMLNAME mainly for downstream systems, and to keep the real property names hidden (for lack of a better word) in the projected data formats).    But at least you got me to where I needed to be.    So you are the Cache Developer Community Hero of the Week - at least for me :)

Best Regards and GO TEAM!

Thanks Enrico.   I look forward to being able to use the new features of Cache and IRIS someday.  The company I work for is still running Cache 2017.1.3, so those new JSON features are not available.  But I sincerely appreciate the information.  Because once we upgrade to IRIS, then I will be able to leverage/use the information you shared.

Best Regards!