are there any cf/lucee functions that can convert ...
# lucee
m
are there any cf/lucee functions that can convert json into xml ?
a
well, beyond... deserialising the JSON and then... running some code over it to implement whatever mapping you need to do for your XML implementation, what are you expecting CFML to do for you here?
IE: your usage of the word "convert" is doing a lot of work in that sentence.
s
as adam said, all you're doing here is de-serializing JSON into a CF struct, and then converting a CF struct to XML. we have a slightly modified version of one of the various tools for this that was on cflib a while back, which is this:
Copy code
/**
	*  @param input      Data to convert into XML. (Required)
 	* @param element      Used to name the root element. (Required)
	* @param attributeMeta Checks for the presence of a key in attributeMeta when rendering the key in input, and writes out the contents as attribute/value pairs for the corresponding key
 	* @return Returns a string. 
 	* @author Phil Arnold (<mailto:philip.r.j.arnold@googlemail.com|philip.r.j.arnold@googlemail.com>) with modifications by Samuel Knowlton (<mailto:sam@inleague.io|sam@inleague.io>) - removed lcasing and added support for attributeMeta
 	* @version September 9, 2009 / September 29, 2021 
 	**/
   
	public string function toXML( required input, required string element, struct attributeMeta = {} ) {
		var i = 0;
		var s = createObject( "java", "java.lang.StringBuilder" ).init()
		var s1 = "";
		s1 = arguments.element;
		if ( right( s1, 1 ) eq "s" ) {
			s1 = left( s1, len( s1 ) - 1 );
		}

		s.append( renderXMLAttribute( arguments.element, arguments.attributeMeta ) );
		if ( isArray( arguments.input ) ) {
			for ( i = 1; i lte arrayLen( arguments.input ); i = i + 1 ) {
				if ( isSimpleValue( arguments.input[ i ] ) ) {
					s.append( renderXMLAttribute( s1, attributeMeta ) & arguments.input[ i ] & "</#s1#>" );
				}
				else {
					s.append( toXML( arguments.input[ i ], s1 ) );
				}
			}
		}
		else if ( isStruct( arguments.input ) ) {
			for ( i in arguments.input ) {
				if ( isSimpleValue( arguments.input[ i ] ) ) {
					s.append( renderXMLAttribute( i, attributeMeta ) & arguments.input[ i ] & "</#i#>" );
				}
				else {
					s.append( toXML( arguments.input[ i ], i ) );
				}
			}
		}
		else {
			s.append( xmlFormat( arguments.input ) );
		}
		s.append( "</#arguments.element#>" );

		return s.toString();
	}

	/**
	 * renderXMLAttribute
	 * @hint A totally low-rent way to render out XML attributes (e.g. <foo bar="baz">) by checking for the presence of the key in a "sister struct" containing attribute meta nodes. Can't handle multiple nodes with the same name.
	 * @return String OPENING attribute only (e.g. just <foo bar="baz">)
	 */

	public string function renderXMLAttribute( required string key, required struct attributeMeta ) {
		var attr = '<#arguments.key#';
		if ( attributeMeta.keyExists( arguments.key ) ) {
			for ( var metaKey in arguments.attributeMeta[ arguments.key ].keyArray() ) {
				attr &= ' #metaKey#="' & arguments.attributeMeta[ arguments.key ][ metaKey ] & '"';
			}
		}
		attr &= '>';
		return attr;
	}
m
thanks all 🙏
a
Wow that is def not how I would build XML in CFML
Just to contextualise my answer to you @mauzer... your question is kinda of like if you received a box of flat-pack furniture pieces in a delivery and yer asking "is there a standard operation to convert this into some furniture". No. It's not a non-sensical request.
m
hmm i dont think it is 😛
a
I can't change what you think. But equally it doesn't mean it's right. It's not. [shrug]. I think you need to think through what yer asking a bit more, and in the context of the flatpack example I used.
This possibly demonstrates my point with code:
Copy code
json = serializeJson({key="value"})
xml = xmlParse("<nowItIsXml>#encodeForXml(json)#</nowItIsXml>")

writeDump([
    json = json,
    xml = xml
])
But that's not useful is it? Why? Cos it doesn't have the XML schema you need, and doubtlessly also needs to think about the schema of the JSON as well. So... right... "are there any cf/lucee functions that can convert json into xml ?" is not a well-informed question. CFML has functions for handling JSON. It also has functions for working with XML. But this is not going to help you transform your JSON to your XML. And no-one here can given an informed answered as to how to deal with either of those with only "are there any cf/lucee functions that can convert json into xml ?" to work with.
Also it's probably best answered with "did you look at the docs? Like searching the docs for "json" and "xml"? Cos... like... they're all right there if you apply yerself _just a bit_".
d
@mauzer The point @Adam Cameron is trying to bring up is that there's no unified expectation of what converting JSON to XML would look like. Your XML needs a schema to be understood, so you need to know what the final XML output is supposed to look like to answer the question properly. For example, if you said something like: Is there a way to built-in way to translate JSON into the following XML schema: <serialized><string name="x">hello</string><object name="y"><key name="a">1</key></object></serialized> That's the kind of detail he's saying is missing.
❤️ 1
Or you could have presented your actual problem: I need to convert something from JSON into an XML structure that is used in XYZ. Is there a built-in function to help me?
👍 1
Also, explaining why you believe you need to convert JSON into XML would be useful too. Maybe there's a different approach that could be used.
👍 1