Saving JSON files

Prev Next

Introduction

Three approache 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 arrays The table is modeled as a nested (i.e. 2-dimensional) array where the 1st row contains the header names
JSON objects The table is modeled as a simple (i.e. 1-dimensional) which contains objects. Header names translate to object names and the data below to object values.
JSON lean Similar to JSON objects, with only difference that blank data entries are not mentioned in the JSON file.

Using 'JSON arrays' approach

In this appraoch, the JSON files uses two distinct names:

  • Object name called "name" to include the table name
  • Object name called "table" to include the contents

  table initialize( table 1, {{ Last Name, 1st Name, Age},
      { Miller, Michelle, 30 }, { Johnson, Jane, 40 }, { '', Nick, 50 }} );

  table initialize( table 2, {{ Pet, Hair color},
      { Dog, Beige }, { Cat, '' }, { Mouse, Gray }} );

  echo("table 1:");
  table list( table 1 );

  echo("table 2:");
  table list( table 2 );

  echo(new line, "Save both tables as JSON arrays" );
  table save( { table 1, table 2} , "Output.json", JSON arrays );
  table load( text, "Output.json", TEXT );

  echo("table 1 & 2 as JSON arrays:_");
  table list( text );
table 1:
    0 : Last Name | 1st Name | Age
    1 : Miller    | Michelle | 30
    2 : Johnson   | Jane     | 40
    3 :           | Nick     | 50

table 2:
    0 : Pet   | Hair color
    1 : Dog   | Beige     
    2 : Cat   |           
    3 : Mouse | Gray      


Save both tables as JSON arrays
table 1 & 2 as JSON arrays:_
    0 : // JSON File format.  Contains table(s). Reference: https://www.json.org
    1 : // Created with B4P                                                      
    2 :                                                                          
    3 : {                                                                        
    4 :     "name" : "table 1",                                                  
    5 :     "table" :                                                            
    6 :     [                                                                    
    7 :       [ "Last Name", "1st Name", "Age" ],                                
    8 :       [ "Miller", "Michelle", 30 ],                                      
    9 :       [ "Johnson", "Jane", 40 ],                                         
   10 :       [ "", "Nick", 50 ]                                                 
   11 :     ]                                                                    
   12 :     "name" : "table 2",                                                  
   13 :     "table" :                                                            
   14 :     [                                                                    
   15 :       [ "Pet", "Hair color" ],                                           
   16 :       [ "Dog", "Beige" ],                                                
   17 :       [ "Cat", "" ],                                                     
   18 :       [ "Mouse", "Gray" ]                                                
   19 :     ]                                                                    
   20 : }                                                                        
   21 :                                                                          

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

Using 'JSON objects' and 'JSON lean' approach

In this appraoch, the JSON files uses two distinct names:

  • Object name called "name" to include the table name
  • Object name called "table" to include the contents

AttentionYou need to ensure that the all header names in the table are unique. Otherwise the JSON contains some confusing data with mulitple values assigned to same JSON names.

  table initialize( table 1, {{ Last Name, 1st Name, Age},
      { Miller, Michelle, 30 }, { Johnson, Jane, 40 }, { '', Nick, 50 }} );

  table initialize( table 2, {{ Pet, Hair color},
      { Dog, Beige }, { Cat, '' }, { Mouse, Gray }} );

  echo("table 1:");
  table list( table 1 );

  echo("table 2:");
  table list( table 2 );

  echo(new line, "Save both tables as JSON objects" );
  table save( { table 1, table 2} , "Output.json", JSON objects );
  table load( text, "Output.json", TEXT );

  echo("table 1 & 2 as JSON objects");
  table list( text );

  echo(new line, "Save both tables as JSON objects" );
  table save( { table 1, table 2} , "Output.json", JSON lean );
  table load( text, "Output.json", TEXT );

  echo("table 1 & 2 as JSON objects:");
  table list( text );

  file delete silently( Output.json );
table 1:
    0 : Last Name | 1st Name | Age
    1 : Miller    | Michelle | 30
    2 : Johnson   | Jane     | 40
    3 :           | Nick     | 50

table 2:
    0 : Pet   | Hair color
    1 : Dog   | Beige     
    2 : Cat   |           
    3 : Mouse | Gray      


Save both tables as JSON objects
table 1 & 2 as JSON objects
    0 : // JSON File format.  Contains table(s). Reference: https://www.json.org
    1 : // Created with B4P                                                      
    2 :                                                                          
    3 : {                                                                        
    4 :     "name" : "table 1",                                                  
    5 :     "table" :                                                            
    6 :     [                                                                    
    7 :       { "Last Name" : "Miller", "1st Name" : "Michelle", "Age" : 30 },   
    8 :       { "Last Name" : "Johnson", "1st Name" : "Jane", "Age" : 40 },      
    9 :       { "Last Name" : "", "1st Name" : "Nick", "Age" : 50 }              
   10 :     ]                                                                    
   11 :     "name" : "table 2",                                                  
   12 :     "table" :                                                            
   13 :     [                                                                    
   14 :       { "Pet" : "Dog", "Hair color" : "Beige" },                         
   15 :       { "Pet" : "Cat", "Hair color" : "" },                              
   16 :       { "Pet" : "Mouse", "Hair color" : "Gray" }                         
   17 :     ]                                                                    
   18 : }                                                                        
   19 :                                                                          


Save both tables as JSON objects
table 1 & 2 as JSON objects:
    0 : // JSON File format.  Contains table(s). Reference: https://www.json.org
    1 : // Created with B4P                                                      
    2 :                                                                          
    3 : {                                                                        
    4 :     "name" : "table 1",                                                  
    5 :     "table" :                                                            
    6 :     [                                                                    
    7 :       { "Last Name" : "Miller", "1st Name" : "Michelle", "Age" : 30 },   
    8 :       { "Last Name" : "Johnson", "1st Name" : "Jane", "Age" : 40 },      
    9 :       { "1st Name" : "Nick", "Age" : 50 }                                
   10 :     ]                                                                    
   11 :     "name" : "table 2",                                                  
   12 :     "table" :                                                            
   13 :     [                                                                    
   14 :       { "Pet" : "Dog", "Hair color" : "Beige" },                         
   15 :       { "Pet" : "Cat" },                                                 
   16 :       { "Pet" : "Mouse", "Hair color" : "Gray" }                         
   17 :     ]                                                                    
   18 : }                                                                        
   19 :                                                                          

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

See also

table save
Loading JSON files