variable load ...

Prev Next

Function Names

variable load

Description

This function loads a JSON file with structured data into a variable.


If the specified variable is existing then all contents will be deleted before the loading procedure starts. The members (and sub-members) will be built up while loading / reading in is in progress.

Call as: procedure

Restrictions

Indirect parameter passing is disabled

Parameter count

2, 3

Parameters

No.TypeDescription
1, etc.
code
variable
:string
Destination variable name

The contents will be loaded into this variable.

2
input
string File name

The file must refer to an existing JSON file. The JSON file may optionally contain a plain array with the starting symbol '[' instead of '{'.

Opt. 3
input
string JSON Format

Following values are supported: JSON: Load JSON files normally. Since JSON does not support data in intermediate nodes (vs. B4P variable model which does), the the beginning and intermediate base veriables will be void.
JSON B4P: Load JSON files wich has previously been saved with variable save() where the data is organized in the JSON format in order to include the data in intermediate nodes.

Default value: JSON

Examples


       pets[] = "All my pets";
       structure( pets[], { Cat, Dog, { Shepard, Dachshund }, Bird,  { Parrot,  Parakeet } },
                          { Katze, Hund, { Schäfer, Dackel }, Vogel, { Papagei, Sittich } } );
       array( pets[Cat], { Hauskatze, Puma, Tiger } );

       see( pets[] );

       variable save( pets[], "Outputs\pets.json" );

       // With 'All my pets' defined in the base variable, the JSON code will be a 'plain array'
       // beginning with '[' and not with '{^'.

       table load( t, "Outputs\pets.json", TEXT ); // Show the JSON file
       table list( t );

       echo("Load the JSON file, assuming it originated from B4P output");
       variable load( animals[], "Outputs\pets.json", JSON B4P );

       see( animals[] ); // You will see that all contents are preserved.

       echo("Now load the JSON file as if the file originated from somewhere else ...");

       // B4P saves intermediate nodes in intermediate arrays containing 2 elements:
       // Element 0:  The base variable at that level
       // Element 1:  Further structured data

       variable load( animals[], "Outputs\pets.json", JSON );
       see( animals[] );

Output

pets[]                  All my pets                (quoted string,full access)
Bird                    Vogel                      (softquoted string,full access)
  Parakeet              Sittich                    (softquoted string,full access)
  Parrot                Papagei                    (softquoted string,full access)
Cat                     Katze                      (softquoted string,full access)
  Array [   0]          Hauskatze                  (softquoted string,full access)
  Array [   1]          Puma                       (softquoted string,full access)
  Array [   2]          Tiger                      (softquoted string,full access)
Dog                     Hund                       (softquoted string,full access)
  Dachshund             Dackel                     (softquoted string,full access)
  Shepard               Schäfer                    (softquoted string,full access)

    0 :                                
    1 : [ "All my pets",{              
    2 :     "Bird" :                   
    3 :     [ "Vogel",{                
    4 :         "Parakeet" : "Sittich",
    5 :         "Parrot" : "Papagei"   
    6 :       }                        
    7 :       ],                       
    8 :     "Cat" :                    
    9 :     [ "Katze",[                
   10 :         "Hauskatze",           
   11 :         "Puma",                
   12 :         "Tiger"                
   13 :       ]                        
   14 :       ],                       
   15 :     "Dog" :                    
   16 :     [ "Hund",{                 
   17 :         "Dachshund" : "Dackel",
   18 :         "Shepard" : "Schäfer"  
   19 :       }                        
   20 :       ]                        
   21 :   }                            
   22 :   ]                            

Load the JSON file, assuming it originated from B4P output
animals[]               All my pets                (quoted string,full access)
Bird                    Vogel                      (quoted string,full access)
  Parakeet              Sittich                    (quoted string,full access)
  Parrot                Papagei                    (quoted string,full access)
Cat                     Katze                      (quoted string,full access)
  Array [   0]          Hauskatze                  (quoted string,full access)
  Array [   1]          Puma                       (quoted string,full access)
  Array [   2]          Tiger                      (quoted string,full access)
Dog                     Hund                       (quoted string,full access)
  Dachshund             Dackel                     (quoted string,full access)
  Shepard               Schäfer                    (quoted string,full access)

Now load the JSON file as if the file originated from somewhere else ...
animals[]               [Void]                     (void,full access)
Array [   0]            All my pets                (quoted string,full access)
Array [   1]            [Void]                     (void,full access)
  Bird                  [Void]                     (void,full access)
    Array [   0]        Vogel                      (quoted string,full access)
    Array [   1]        [Void]                     (void,full access)
      Parakeet          Sittich                    (quoted string,full access)
      Parrot            Papagei                    (quoted string,full access)
  Cat                   [Void]                     (void,full access)
    Array [   0]        Katze                      (quoted string,full access)
    Array [   1]        [Void]                     (void,full access)
      Array [   0]      Hauskatze                  (quoted string,full access)
      Array [   1]      Puma                       (quoted string,full access)
      Array [   2]      Tiger                      (quoted string,full access)
  Dog                   [Void]                     (void,full access)
    Array [   0]        Hund                       (quoted string,full access)
    Array [   1]        [Void]                     (void,full access)
      Dachshund         Dackel                     (quoted string,full access)
      Shepard           Schäfer                    (quoted string,full access)

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

See also

variable save
json to variable