October 16, 2024

JSON is a standard used in multiple areas like integrations between systems (like Power Automate connections, for example), so it’s pretty widespread even if we don’t notice we’re using it. So let’s look at the Power Automate “json “function and how it’s useful.

A bit of fundamentals before

JSON has a fixed structure, so we know what the function expects. I won’t go over much detail here about it, but I’ll write about that soon; JSON follows a key/value pair structure. So, if we call something by a name, we get a value back, called an object. Of course, it’s a lot more complex than this, but it’s a good start for us. Here’s an example of an object:

{
	"key":"value"
}

So we can build the following object:

{
	"name":"Manuel T. Gomes"
}

So if we want the name, we’ll get “Manuel T. Gomes”. Why is this useful? Because we can define multiple and have a standard list of names. We call that an array, meaning that it contains a bunch of elements inside. Here’s an example:

[
	{
		"name":"Manuel T. Gomes"
	},
	{
		"name":"Jack the Man"
	}
]

So above, we have two elements in an array. In the case of Power Automate, we can use a “Apply to each” action and get the names by referring to them as “name”. You’re doing this a lot without even noticing since Power Automate makes things a bit clearer and hides the complexity.

Please note that the tabs in the string are only for us better understand the structure. We can provide, as you’ll see below, a string without tabs or returns and it will work

Again, this is an oversimplification, but you get the picture. So now, with a bit of fundamental out of the way, let’s look at the Power Automate “json “function.

Usage

It follows a simple pattern.

  1. String to convert

For example:

json('{"name":"Manuel T Gomes"}')

will return 

{
  "name": "Manuel T Gomes"
}

We’ll get two things:

  1. Validation that the JSON is valid
  2. Formatting of the JSON so that we can understand it better.

Let’s provide an invalid JSON by “forgetting” one of the double quotes (the one at the end of my name).

json('{"name":"Manuel T Gomes}')

will return an error

Here’s the error:

Unable to process template language expressions in action 'Compose' inputs at line '1' and column '6312': 'The template language function 'json' parameter is not valid. The provided value '{"name":"Manuel T Gomes}' cannot be parsed: 'Unterminated string. Expected delimiter: ". Path 'name', line 1, position 24.'. Please see https://aka.ms/logicexpressions#json for usage details.'.

The error is quite cryptic, but we know what it’s telling us now that we know the fundamentals. It indicates that the value provided (the string) has an error. It even says what is the problem with the Expected delimiter: “. In this case, it’s easy to look at the string and understand the error, but if the JSON is big, it’s a lot trickier. I recommend using sites like jsonlint that will format the JSON and tell you of any problem.

How about if the string is null?

json(null)

will return an error

Here’s the error:

Unable to process template language expressions in action 'Compose' inputs at line '1' and column '4714': 'The template language function 'json' expects its parameter to be a string or an XML. The provided value is of type 'Null'. Please see https://aka.ms/logicexpressions#json for usage details.'.

Notice that “null” is not the same as empty. Here’s what happens when we provide an empty string:

json('')

will return an empty object

The Flow won’t crash, and the json function will return an empty object.

XML

Aside from a string, we can also provide an XML. If you’re not familiar with XML, it’s another standard representation of data. Here’s an example:

<person>
	<name>Manuel T. Gomes</name>
</person>

It’s similar to what de defined before, but it follows a tag open and close strategy. In the case above, the object “person” has one property called “name.” We defined the start and end by the “<>” and “</>” respectively. There are a lot of rules and details about XML, but what’s essential to this article it’s used in the json function. So let’s provide the string above and see what happens.

json(xml('<person><name>Manuel T. Gomes</name></person>'))

will return 

{
  "person": {
    "name": "Manuel T. Gomes"
  }
}

I made this for a reason. So that you can see in action that a “key” needs to be a string, but the value can be another object. It’s pretty powerful to have this because you can have complex objects that encapsulate a lot of information.

So what’s happening above? We have an object called “person” with another object as the value. Let’s expand a bit the example to understand why this is useful entirely:

json(xml('<person><name>Manuel T. Gomes</name><address>the beach</address></person>'))

will return 

{
  "person": {
    "name": "Manuel T. Gomes",
    "address": "the beach"
  }
}

It “makes sense”. The object person has two properties called “name” and “address”. You can insert more objects, and it will provide you with more information about the person, like:

json(xml('<person><name>Manuel T. Gomes</name><address>the beach</address><age>40</age></person>'))

will return 

{
  "person": {
    "name": "Manuel T. Gomes",
    "address": "the beach",
	"age":40
  }
}

It looks good, right (except the age, I’m getting old 😢)?

Limitations

I’ve provided a massive string in the past, and the json function coped with it, so I can’t say I found a limitation to this function. But if you do, please let me know via email or Twitter

Recommendations:

Here are some things to keep in mind.

Don’t nest

There’s no real reason to do it. If you nest Power Automate “json “function, you’ll get an error.

Unable to process template language expressions in action 'Compose' inputs at line '1' and column '6309': 'The template language function 'json' parameter is not valid. The provided value '{"name":"Manuel T Gomes}' cannot be parsed: 'Unterminated string. Expected delimiter: ". Path 'name', line 1, position 24.'. Please see https://aka.ms/logicexpressions#json for usage details.'.

Sources:

Microsoft’s json Function Reference

Back to the Power Automate Function Reference.

Photo by Pankaj Patel on Unsplash

Manuel Gomes

I have 18 years of experience in automation, project management, and development. In addition to that, I have been writing for this website for over 3 years now, providing readers with valuable insights and information. I hope my expertise allows me to create compelling, informative content that resonates with the audience.

View all posts by Manuel Gomes →

Leave a Reply

Your email address will not be published. Required fields are marked *

Mastodon