CRM Crate

Understanding JSON function in Power Apps

We will learn and understand the usage of JSON function in Power Apps. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Power Apps.

Understanding JSON function in Power Apps

What is JSON?

The term “JSON” stands for JavaScript Object Notation. It is a lightweight format for storing and transporting data and is often used when data is sent from a server to a web page.

JSON Syntax Rules

1. Data is in name/value pairs
2. Data is separated by commas
3. Curly braces hold objects
4. Square brackets hold arrays

The below example defines an animal object: an array of 3 animal records (objects):

{
"animal":[
    {"name":"Elephant", "region":"India"},
    {"name":"Tiger", "region":"India"},
    {"name":"Kangaroo", "region":"Australia"}
]
}

Understanding JSON() function in Power Apps

The Power FX’s JSON function returns the JavaScript Object Notation representation of a data structure as text so that it’s suitable for storing or transmitting across a network. ECMA-404 and IETF RFC 8259 describe the format, which is widely used by JavaScript and other programming languages.

JSON(DataStructure [, Format])

1. DataStructure – Required. The data structure to convert to JSON. Tables, records, and primitive values are supported, arbitrarily nested.
2. Format – Optional. JSONFormat enum value. The default value is Compact, which doesn’t add newlines or spaces and blocks binary data and unsupported columns.

There are multiple JSONFormat enums available in Power Apps. You can specify the optional “Format” argument / parameter to control how readable the result is and how unsupported and binary data types are handled. By default, the JSON output is set as compact with no unnecessary spaces or newlines, and unsupported data types and binary data aren’t allowed. You can combine multiple formats if you specify the & operator.

JSONFormat enumDescription
CompactDefault. The output is as compact as possible with no added spaces or newlines.
IndentFourTo improve readability, the output contains a newline for each column and nesting level and uses four spaces for each indentation level.
IncludeBinaryDataThe result includes image, video, and audio-clip columns. This format can dramatically increase the result’s size and degrade your app’s performance.
IgnoreBinaryDataThe result doesn’t include image, video, or audio-clip columns. If you specify neither IncludeBinaryData nor IgnoreBinaryData, the function produces an error if it encounters binary data.
IgnoreUnsupportedTypesUnsupported data types are allowed, but the result won’t include them. By default, unsupported data types produce an error.

Covert a Power Apps Collection into JSON

In our scenario, we will convert the collection’s data into JSON. In the below given example, we have first created a new collection named “CRMCrateData” as given below.

ClearCollect( CRMCrateData,
    { UserName: "Prashant Tirlotkar",    Country: "India", ID: 123 },
    { UserName: "Omkar Rane",    Country: "India", ID: 456 },
	{ UserName: "Priya Gupta",    Country: "India", ID: 789 },
	{ UserName: "Srushti Deshmukh",    Country: "India", ID: 333 },
	{ UserName: "Ishita Kothari",    Country: "India", ID: 444 },
	{ UserName: "Sourav Pal",    Country: "India", ID: 555 },
	{ UserName: "Prashant Miraje",    Country: "India", ID: 666 },
	{ UserName: "Vipul Kithani",    Country: "India", ID: 777 },
	{ UserName: "Vrinda Patel",    Country: "India", ID: 888 }
);

Later, a new variable is created which stores the converted JSON data as given below.

Set(varJSON, JSON(CRMCrateData,Compact))

The below animations demonstrate the implementation of Collection to JSON transformation in Power Apps.

JSON function in Power Apps

Thus, we understood the JSON function & learned to convert a Collection into JSON in Power Apps.

0 0 votes
Article Rating
Subscribe
Notify of

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Improve user experience with SetFocus function in Power App - CRM Crate
1 month ago

[…] user experience with SetFocus function in Power App. Before we start, make sure to subscribe to CRM Crate so that you can stay up to date in the field of Power […]

error: CRM Crate Security Engine - Disabled Right Click & Selection!

Congratulations!

Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.