Application Development and Automation Blog Posts
Learn and share on deeper, cross technology development topics such as integration and connectivity, automation, cloud extensibility, developing at scale, and security.
cancel
Showing results for 
Search instead for 
Did you mean: 
Dan_Wroblewski
Developer Advocate
Developer Advocate
524

I wouldn't have thought this would be a big deal, but I recently got a call from a colleague in Ireland who was building an app for an important customer. And the customer had a requirement to retrieve data and display it in formatted JSON.

So my colleague asked: How do you you display formatted JSON?

At first, we both thought you could simply use the ENCODE_JSON formula function and then display in a Text field, but that did not work. But then I discovered (with the help of a friend 😽) that that function has a parameter, and that is all you need.

How to Do It

To demonstrate, I connected to the Northwind Product entity.

Dan_Wroblewski_0-1745382822429.png

I then created a data variable that returned the first product, again, just to have some data.

Dan_Wroblewski_1-1745382908419.png

On my UI, I created a container with a text box, whose content was set to a page variable, textToDisplay (with an initial value). I used the built-in style Primary Card to make it look nice.

Dan_Wroblewski_3-1745383303330.png

SIDE NOTE: I used the custom width/height setting to make the container a minimum size but let it expand with the data. 

Dan_Wroblewski_4-1745383408114.png

I then added a Set Page Variable in the data variable logic to set the page variable.

Dan_Wroblewski_5-1745383583344.png

And I set the page variable to the formula below. Note some interesting things:

  • If you set the Spaces parameter of ENCODE_JSON, you will get the JSON nicely formatted.
  • Again, notice the formula editor has really good documentation on every function (if I would only read it 🤦).
ENCODE_JSON(data.Products1,4)

Dan_Wroblewski_2-1745383117365.png

The result?

Dan_Wroblewski_6-1745383877050.png

Without the Spaces parameter, it would have displayed like this:

Dan_Wroblewski_7-1745383977323.png

Making It Editable

You could also put the text in an input box so a user could edit it.

Dan_Wroblewski_0-1745385085098.png

Then you could provide a button to validate the JSON they type by using a simple JavaScript flow function.

I set the input to the page variable, changed the output to true/false, and used the following code:

Dan_Wroblewski_1-1745385189838.png

If I click Validate with the default JSON:

Dan_Wroblewski_2-1745385248719.png

If I make an error:

Dan_Wroblewski_3-1745385318730.png

👉 Let me know if this was helpful, and maybe how you would improve on this.

 

 

Labels in this area