DEV Community

Barret Blake
Barret Blake

Posted on • Originally published at barretblake.dev on

Function Friday: Data Type Conversion Functions – Arrays, JSON and XML

This time we’re wrapping up the last of the data type conversion functions. We’ll cover the more complex data types: arrays, JSON, and XML.

array

The array function creates an array from a single input data element. The format is simple:

array('<value>')
Enter fullscreen mode Exit fullscreen mode

And as an example:

array('hello there') // returns ["hello there"]
Enter fullscreen mode Exit fullscreen mode

To be honest, there’s no reason to use the array function. The createArray function allows you to pass in multiple data elements and you get the same result.

createArray

The createArray works the same way as array, but allows you to pass in multiple input elements, returning them as a single array. The format is as follows:

createArray('<value1>', '<value2',...)
Enter fullscreen mode Exit fullscreen mode

And here is an example:

createArray('hello', 'there') //returns ["hello","there"]
Enter fullscreen mode Exit fullscreen mode

As I mentioned above, you should pretty much always use this function instead of array, just for consistency’s sake. As with any array in Power Automate, the values can be of different types.

createArray('hello', 25, 'there') // returns ["hello",25,"there"]
Enter fullscreen mode Exit fullscreen mode

json

This function will take your input and return it as a JSON formatted object. The pattern is as follows:

json('<value>')
Enter fullscreen mode Exit fullscreen mode

The value passed in must be a string or XML. And if you’re passing in XML, you’ll need to pair it with the xml function to parse the XML data so the json function understands it. For example:

json(xml('YOUR XML DATA'))
Enter fullscreen mode Exit fullscreen mode

This function is most useful for parsing JSON data returned from an API or file so that Power Automate can treat it as a JSON data type instead of treating it as a string.

An example:

json('{"obiWanSays": "Hello there!"}') // returns { "obiWanSays": "Hello there!" }
Enter fullscreen mode Exit fullscreen mode

If you’re looking to take disparate data elements and build a JSON object from them, take a look at the compose action in Power Automate instead.

xml

As with the json function, the xml function is used for parsing xml being passed in as a string into an object that Power Automate will understand to be XML. The format is similar to the others:

xml('<value>')
Enter fullscreen mode Exit fullscreen mode

As with the json function, this will most often be used to parse data returned from an API or a file that is XML in nature. And the data being passed in must be a string or a JSON data object. Also like the json function, if the data being passed in is JSON in a string, it should be paired with that function. Like so:

xml(json('<JSON DATA OBJECT>'))
Enter fullscreen mode Exit fullscreen mode

This will take the JSON string data, parse the JSON, then convert it into an XML object.

Conclusion

This wraps up the last of the data type conversion functions. Coming up next I’ll start delving into one of the biggest pain points in Power Automate (and almost any coding environment): date and time.

The post Function Friday: Data Type Conversion Functions – Arrays, JSON and XML first appeared on Barret Codes.

Top comments (0)