json.array.append ( )

Function stats

Average user rating
37
414
9999
Support
FileMaker 10.0 +
Date posted
18 November 2017
Version
1
Recursive function
No

Author Info
 Fabrice

76 functions

Average Rating 4.4

author_avatar



 

Function overview

Prototype

json.array.append  ( _value;   _type;   _init or end )


Parameters

_value  


_type  default is JSONString


_init or end  


Description

Tags:  json   array  

appends values to a JSON array.

Examples

Sample input

json.array.append ( "a" ; 1 ; "INIT" ) & json.array.append ( "b" ; 1 ; "" ) & json.array.append ( "c" ; 1 ; "END" )
json.array.append ( "a" ; 1 ; "INIT" ) & json.array.append ( "x" ; 1 ; "INIT" ) & json.array.append ( "y" ; 1 ; "" ) & json.array.append ( "z" ; 1 ; "END" ) & json.array.append ( "b" ; 1 ; "" ) & json.array.append ( "c" ; 1 ; "END" )


Sample output

["a","b","c"]
["a",["x","y","z"],"b","c"]

 

Function code

/* json.array.append ( _value ; _type ; _init or end )

by Fabrice Nordmann & Andries Heylen, 1-more-thing
@1morethingtweet


appends values to a JSON array.

Simple array:
json.array.append ( "a" ; 1 ; "INIT" ) & json.array.append ( "b" ; 1 ; "" ) & json.array.append ( "c" ; 1 ; "END" ) --> ["a","b","c"]

Nested
json.array.append ( "a" ; 1 ; "INIT" ) & json.array.append ( "x" ; 1 ; "INIT" ) & json.array.append ( "y" ; 1 ; "" ) & json.array.append ( "z" ; 1 ; "END" ) & json.array.append ( "b" ; 1 ; "" ) & json.array.append ( "c" ; 1 ; "END" ) ---> ["a",["x","y","z"],"b","c"]


V2    Nov 2017
        supports nested arrays
v1    May 2017

*/


Case ( _init or end = "INIT" ;

    /*    --------- INIT ---------
        init a new array object
    */
    
    Let ([
        // store parent array info
        $jsapnd.level = $jsapnd.level + 1 ;
//        $$log = Case ( $jsapnd.level = 1 ; "" ; $$log );

        $jsapnd.index.parent[$jsapnd.level] = 0 + $jsapnd.index ;
        $jsapnd.obj.parent[$jsapnd.level] = $jsapnd.obj ;
//        $$log = List ( $$log ; "-----" ; "init of parent: " & $jsapnd.obj.parent[$jsapnd.level] & " on level " & $jsapnd.level );

        // init a new array
        $jsapnd.index = -1 ; // will be set to 0 (+1) in the main part
        $jsapnd.obj = "[]"
    ];

    "" )


) &


    /*    MAIN - in all cases
        define rank
        save array
    */

Let ([
    _type = Case ( IsEmpty ( _type ) ; JSONString ; _type ) ;
    $jsapnd.index = $jsapnd.index + 1;
    $jsapnd.obj = JSONSetElement ( $jsapnd.obj ; $jsapnd.index ; _value ; _type )
];
    "" )

// & Let ( $$log = List ( $$log ; "-----" ; "level: " & $jsapnd.level ; "index: " & $jsapnd.index ; "obj: " & $jsapnd.obj ) ; "" )

&

Case ( _init or end = "END" ;

    /*    --------- END ---------
        save the current array to the parent array
        clean up
        return result
    */

    Let ([
//        $$log = List ( $$log ; "----" ; "before save: " ; $jsapnd.obj );
//        $$log = List ( $$log ; " save to parrent: " & $jsapnd.obj.parent[$jsapnd.level] & " for level " & $jsapnd.level & " and save to index: " & $jsapnd.index.parent[$jsapnd.level] );
        $jsapnd.obj.parent = Case ( $jsapnd.level <> 1 ; JSONSetElement ( $jsapnd.obj.parent[$jsapnd.level] ; $jsapnd.index.parent[$jsapnd.level] + 1 ; $jsapnd.obj ; JSONArray ) ; $jsapnd.obj ) ;
//        $$log = List ( $$log ; "----" ; "parent after save: " & $jsapnd.obj.parent );

        $jsapnd.obj = Case ( $jsapnd.level <> 1 ; $jsapnd.obj.parent ; $jsapnd.obj ) ;
//        $$log = List ( $$log ; "----" ; "object after save: " & $jsapnd.obj );

        $jsapnd.index = $jsapnd.index.parent[$jsapnd.level] + 1; // will be set to + 1 during the next iteration
        $jsapnd.level = $jsapnd.level - 1 ;
        
        $jsapnd.index.parent[$jsapnd.level+1] = "" ;
        $jsapnd.obj.parent[$jsapnd.level +1] = ""
    ] ;
    Case ( not $jsapnd.level ; Let ([ _result = $jsapnd.obj ; $jsapnd.obj = "" ; $jsapnd.level = "" ; $jsapnd.index = "" ]; _result ))
)

)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/414

    Prototype: json.array.append( _value; _type; _init or end )
    Function Author: Fabrice (http://www.fmfunctions.com/mid/37)
    Last updated: 18 November 2017
    Version: 1

*/
// ===================================