KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Prettify your JSON Stringify in a Text/Field object
PRODUCT: 4D | VERSION: 17 | PLATFORM: Mac & Win
Published On: September 20, 2018

JSON can easily be displayed through text using the JSON Stringify command. By using the "*" parameter, a sample text can look like the following:



4D will space the tabs in which it can make the JSON difficult to read when entered in a Text and/or Field form object. The following utility method can reduce the spacing of a given 4D object:

// -----------------------------------------------------------------
// Name: prettifyJSONText
// Description: Method will make the 4D Object in JSON easier to
// read as text
//
// Input Parameters:
// $1 (OBJECT) - 4D Object as a JSON definition
// Output:
// $0 (TEXT) - Simplier JSON text to read
// ------------------------------------------------------------------
C_OBJECT($1;$inputObj)
C_TEXT($0;$outputJSONText)
If (Count parameters=1)
  $inputObj:=$1
  
  $outputJSONText:=Replace string(JSON Stringify($inputObj;*);\
  Char(Tab);" ")
  $0:=$outputJSONText
End if


Here is the sample of using the method:

C_OBJECT($jsonObj)
C_TEXT($jsonText)

$jsonText:=prettifyJSONText($jsonObj) // Containing a JSON def





Note: If tab characters are part of the value of a given JSON property, they are escaped which will not be replaced with the Replace String command.


See Also: