json to variable
This function loads JSON contents from a text string into a variable. This approach is useful if JSON text has been obtained
from other sources, for example a table.
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.
Indirect parameter passing is disabled
2, 3
No. | Type | Description |
---|---|---|
1, etc. code |
variable :string |
Destination variable name The contents will be loaded into this variable. |
2 input |
string | JSON text The complete JSON code is in this string. The JSON text 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 text 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. |
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[] );
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)
The following code example demonstrates how the different B4P data types are loaded. An automatic recognition of numbers, dates, booleans, etc. is supported.
misc[boolan] = true;
misc[numeral] = 123.45;
misc[date] = date( "2020-07-14 17:00" );
misc[set] = { a, 1, { b, 2 }, c, true, date( "2020-08-01 21:00" ) };
j[] = variable to json( misc[], JSON );
// With 'All my pets' defined in the base variable, the JSON code will be a 'plain array'
// beginning with '[' and not with '{'.
echo("This is the JSON code:", new line, j[], new line, "_End of JSON text ___________");
echo("Load the JSON text, assuming it originated from B4P output");
json to variable( diverse[], j[], JSON B4P );
see( diverse[] ); // You will see that all contents are preserved.
This is the JSON code:
{
"boolan" : true,
"date" : "2020-07-14 17:00:00",
"numeral" : 123.45,
"set" : "{'a',1,{'b',2},'c',true,'2020-08-01 21:00:00'}"
}
_End of JSON text ___________
Load the JSON text, assuming it originated from B4P output
diverse[] [Void] (void,full access)
boolan true (boolean,full access)
date 2020-07-14 17:00:00 "2020-07-14 17:00:00" (date,full access)
numeral 123.45 "123.45" (numeral,full access)
set 6 elements: (set,full access)
a (softquoted string)
1 "1" (numeral)
2 elements: (set)
b (softquoted string)
2 "2" (numeral)
c (softquoted string)
true (boolean)
2020-08-01 21:00:00 "2020-08-01 21:00:00" (date)