-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonToVars.txt
71 lines (64 loc) · 1.85 KB
/
JsonToVars.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
* @SIGNATURE:
* #JsonToVars
*
* @PARAMETERS:
* json
* namespace
*
* @HISTORY:
* Created: 2020-04-13 by Mike Duncan
*
* @PURPOSE:
* Handle setting variables from JSON automatically
*
* @RESULT:
* Zero if no error, or an error code if there was an error setting a variable
*
* @NOTES:
* This function requires JSON functions available in FileMaker 16+
* And also the WHILE function available in FileMaker 18+
* Usage example:
* JsonToVars ( Get ( ScriptParameter ) ; "_" )
*
* then test for errors and show dialog in FM script.
* If [ JsonToVars ( Get ( ScriptParameter ) ; "_" ) ]
* Show Custom Dialog [ "Error" ; "There was an error evaluating variables: " & JSONGetElement ( $json_debug ; "json" ) & " - " & GetErrorDesc_en ( JSONGetElement ( $json_debug ; "json" ) )]
* Exit Script [ Text Result: 5 //invalid command ]
* End If
*
* Example of setting a script parameter:
* JSONSetElement ( ""
*
* ; [ "var1" ; "hello" ; JSONString ]
* ; [ "var2" ; "world" ; JSONString ]
* ; [ "var3" ; "ping" ; JSONString ]
*
* )
*
*/
Case (IsEmpty ( json );
// required value missing, return an error
Let ( $json_debug = JSONSetElement ( "" ; "json" ; "10" ; JSONString ) ; "" )
& $json_debug
;
While (
[
namespace = namespace;
json_keys = JSONListKeys ( json ; "" );
count = ValueCount ( json_keys );
json_err = "";
this_err = 0
];
( count > 0 ) and ( this_err = 0 );
[
this_key = GetValue ( json_keys ; count );
this_value = JSONGetElement ( json ; this_key );
this_evaluate = "Let ( $" & namespace & this_key & " = " & Quote ( this_value ) & " ; \"\")";
this_err = EvaluationError ( Evaluate ( this_evaluate ) );
$json_debug = JSONSetElement ( json_err ; [ this_key ; this_err ; JSONString ] ; [ "json" ; this_err ; JSONString ] );
count = count - 1
];
this_err
)
)