Loading JSON files

Prev Next

Loading whole JSON files

Whole json files can be loaded into tables using a similar structural decomposition as applied with loading XML files. B4P loads complete XML files. The XML file will be put into a structured table. The table will contain following header names:

JSON Level Nesting level. Begins with 0
JSON Name For objects, the object name is listed.
For simple values, value name is listed. In all other cases, this field is blank.
JSON Index For arrays, the index number is listed, otherwise it is blank.
JSON Type One of:object, array, or identified type such as numeral, string, date, boolean, set and vodi. The null value translates to void.
JSON Value Corresponding values. Fields are blank for object and array declarations as values follow in the rows below

       table load( t, "Examples\Example1.json", TEXT );

       echo("This is the JSON file:");
       table list( t );

       table load( t, "Examples\Example1.json", JSON );

       echo; echo;
       echo("This is the loaded table from the JSON file:");
       table list( t );
This is the JSON file:
    0 : {                                                                                   
    1 :   "table_0" :                                                                       
    2 :   { "name" : "My Pals", "table" :                                                   
    3 :     [                                                                               
    4 :       [ "Last Name", "First Name", "Age" ],  [ "Miller", "Michelle", 30 ],          
    5 :       [ "Johnsson", "Jane", 40 ],            [ "", "Nick", 50 ]                     
    6 :     ]                                                                               
    7 :   },                                                                                
    8 :   "table_1" :                                                                       
    9 :   { "name" : "My Animals", "table" :                                                
   10 :     [                                                                               
   11 :       { "Pet" : "Dog", "Color" : "Beige" },  { "Color" : "Black", "Pet" : "Cat" }   
   12 :     ]                                                                               
   13 :   },                                                                                
   14 :   "Misc" :                                                                          
   15 :   {                                                                                 
   16 :       "Dates" : "2020-12-31", "Bools" : true, "Voids" : null, "Sets" : "{a,b,1,2,3}"
   17 :   }                                                                                 
   18 :                                                                                     
   19 : }                                                                                   
   20 :                                                                                     



This is the loaded table from the JSON file:
    0 : JSON Level | JSON Name | JSON Index | JSON Type | JSON Value       
    1 : 0          |           |            | object    |                  
    2 : 1          | table_0   |            | object    |                  
    3 : 2          | name      |            | string    | My Pals          
    4 : 2          | table     |            | array     |                  
    5 : 3          |           | 0          | array     |                  
    6 : 4          |           | 0          | string    | Last Name        
    7 : 4          |           | 1          | string    | First Name       
    8 : 4          |           | 2          | string    | Age              
    9 : 3          |           | 1          | array     |                  
   10 : 4          |           | 0          | string    | Miller           
   11 : 4          |           | 1          | string    | Michelle         
   12 : 4          |           | 2          | numeral   | 30               
   13 : 3          |           | 2          | array     |                  
   14 : 4          |           | 0          | string    | Johnsson         
   15 : 4          |           | 1          | string    | Jane             
   16 : 4          |           | 2          | numeral   | 40               
   17 : 3          |           | 3          | array     |                  
   18 : 4          |           | 0          | string    |                  
   19 : 4          |           | 1          | string    | Nick             
   20 : 4          |           | 2          | numeral   | 50               
   21 : 1          | table_1   |            | object    |                  
   22 : 2          | name      |            | string    | My Animals       
   23 : 2          | table     |            | array     |                  
   24 : 3          |           | 0          | object    |                  
   25 : 4          | Pet       |            | string    | Dog              
   26 : 4          | Color     |            | string    | Beige            
   27 : 3          |           | 1          | object    |                  
   28 : 4          | Color     |            | string    | Black            
   29 : 4          | Pet       |            | string    | Cat              
   30 : 1          | Misc      |            | object    |                  
   31 : 2          | Dates     |            | date      | 2020-12-31       
   32 : 2          | Bools     |            | boolean   | true             
   33 : 2          | Voids     |            | void      | # Invalid Value #
   34 : 2          | Sets      |            | set       | {'a','b',1,2,3}  

Try it yourself: Open LIB_Features_Loading_JSON_files.b4p in B4P_Examples.zip. Decompress before use.

Loading tables in JSON Files

With the procedure table load(), B4P is able to load arrays embedded in JSON files into tables. Without specifying a text pattern, the first array will be used. If a text pattern is specified, then B4P will skip all contents until the text pattern is found and then parses forward until the next array is found.

Since tables are only two-dimensional objects and not trees, some rules and restrictions apply:

1. The data to load must be inside an array where each array element represents one row: [ ..., ..., ... ]
The elements inside the array may one of further
arrays [ ..., ..., ... ],
objects { ... : ..., ... : ... ], and
simple values like "strings" (in double quotation marks), numerals (if needed: use decimal points, not commas), true, false and null.

2. In case of arrays, the elements correspond to the table columns added from left to right.
Restriction: No further nested arrays or objects are allowed.
Note: If the first row is an array, then it is considered to contain header names.

3. In case of objects, the tuples consist of header names followed by colon (:) followed by value. The column location in the table will be matched with the header name. If the header name is missing, then it will be added to the header row.
Restriction: No further nested arrays or objects are allowed.
Note: If the first row is a collection of objects, then the header names will written into the header row and the data in the 2nd row below.

4. In case of simple values, these values will be written into the first column.

       echo("The same JSON file is loaded as shown in the 1st code example.", new line);
       table load( t, "Examples\Example1.json", JSON TABLE );

       echo; echo;
       echo("First tabe loaded:");
       table list( t );

       table load( t, "Examples\Example1.json", JSON TABLE, "table_1" );

       echo; echo;
       echo("This is the loaded table below table_1:");
       table list( t );
The same JSON file is loaded as shown in the 1st code example.



First tabe loaded:
    0 : Last Name | First Name | Age
    1 : Miller    | Michelle   | 30
    2 : Johnsson  | Jane       | 40
    3 :           | Nick       | 50



This is the loaded table below table_1:
    0 : Pet | Color
    1 : Dog | Beige
    2 : Cat | Black

Try it yourself: Open LIB_Features_Loading_JSON_files_01.b4p in B4P_Examples.zip. Decompress before use.

See also

table load
Loading XML files
Loading HTML files