Hello everyone and welcome to my blog. In this blog I will show how you get the property tag names of dynamic JSON schema and the value of each property.
This blog will be helpful for the following scenarions.
- The JSON schema dynamic and you are unaware of the property names in the JSON schema.
- You need to check if a particular property is present in your JSON object and take decision accordingly.
For this blog, I will go with a very simple example. I have a text input field. I have set the Default value of the text input field to a JSON array. If you observe the schema carefully, the second item in the array has an additional property ‘Contract Type’ which is not there for the first item.
Also let’s assume that this schema is dynamic and I am not aware of the property names. All I am aware of is that the input text field is a valid JSON.
Let’s check how to can identify the JSON properties and their respective values. The first step is to identify the property names in the JSON.
Below is the code to get the properties of the JSON.
Clear(collItemsWiseProperties);
ForAll(
ParseJSON(TextInput1.Text) As untypedArray,
Collect(
collItemsWiseProperties,
Concat(
ColumnNames(untypedArray) As cols,
cols.Value,
","
)
)
)
Observe the usage of the ColumnNames function. This function in PowerApps is used to get the column names/ property tags of the JSON object.
ColumnNames function return an array. I am using the Concat function to get the property names of the JSON object separated by comma.
And finally to print it, I used the below formula.
Concat(
collItemsWiseProperties,
ThisRecord.Value & Char(13)
)
Char(13) is used for new line character in Power Apps.
And below is the output.
As you can see, the second item in the JSON array had four properties/ columns and it printed accordingly. So now even if you have a dynamic schema, you can get easily get the JSON property tags using the ColumnNames function.
But how to get the value for each property? To get that, we can use the Column function. I have modified the code this time to get not only the property name but also the value.
Clear(collItemsWiseProperties);
ForAll(
ParseJSON(TextInput1.Text) As untypedArray,
Collect(
collItemsWiseProperties,
Concat(
ColumnNames(untypedArray) As cols,
$"{cols.Value}: {Column(untypedArray, cols.Value)}",
","
)
)
)
Observe the usage of the Column function. Using ColumnNames I am getting the property name and then using the Column function, I am getting the value of each property.
Below is the output of the collection.
Wonderful isn’t it? Now you don’t need to worry even if your JSON schema is dynamic or you are unaware of the properties at design time.
Hope this helped.
Debajit
Discover more from Debajit's Power Apps & Dynamics 365 Blog
Subscribe to get the latest posts sent to your email.