KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method to output tables and fields to JSON
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: May 31, 2023

Below is a utility method to take all tables and fields in a database and output into the JSON format to the desktop:

ARRAY TEXT($tables_at; 0)
ARRAY LONGINT($tableNums_al; 0)
C_LONGINT($lastFieldNum_l)

GET TABLE TITLES($tables_at; $tableNums_al)

C_COLLECTION($tables_c)
$tables_c:=New collection

For ($i; 1; Size of array($tables_at))
   $table_o:=New object
   $table_o.name:=$tables_at{$i}
   $table_o.num:=$tableNums_al{$i}
  
   $lastFieldNum_l:=Get last field number($i)
  
   // For each field of the current table
   $table_o.fields:=New collection
   For ($j; 1; $lastFieldNum_l)
     $table_o.fields.push(Field name($i; $j))
   End for
  
   $tables_c.push($table_o)
End for

$json_t:=JSON Stringify(New object("tables"; $tables_c); *)
TEXT TO DOCUMENT(System folder(Desktop)+"tables.json"; $json_t)