This post starts with me wanting to format some JSON with line breaks for the user to read. It’s the response from an Azure Function which integrates with a local SQL server (interesting subject, maybe for another time). The result from SQL server is serialized into a (potentially very long) JSON string and this is the string that I want to present in a more human-readable format.
If you’re still here then here are the stations that the train of thought pulled into, starting with the worst.
Have some control on my page for the user to view the JSON returned from the Azure Function, formatted with line breaks.
1. Format at Source
Why not just add the line breaks when I am serializing the results in the C# of my Azure Functions? That way I don’t need to change anything in AL.
No, that’s dumb. That would make every response from the function larger than it needs to be just for the rare occasions when a human might want to read it. Don’t do that.
2. Call an Azure Function to Format the Result
I could have a second Azure Function to accept the unformatted result and return the formatted version. I could have a Function App which runs node.js and return the result in a couple of lines of code.
3. Create a User Control
4. Use WebPageViewer
The WebPageViewer has a
If you pass some script it will be executed when the page control is loaded. Perfect for what I need. I can just use the JSON.parse and JSON.stringify functions to read and then re-format my JSON text. I’m also wrapping it in
The AL code ends up looks like this:
local procedure SetResult(NewResult: Text) var JS: Text; begin NewResult := NewResult.Replace('''', ''); JS := StrSubstNo('document.write(''<pre>'' + JSON.stringify(JSON.parse(''%1''), '''', 2) + ''</pre>'');', NewResult); CurrPage.ResultsCtrl.SetContent('', JS); end;
If you’re not using 26 single quotes in three lines of code then you’re not doing it right 😉