Support for JSONSetElement with empty path

Idea created by mrwatson-gbs on Jun 27, 2018
    Active
    Score5
    • Benjamin Fehr
    • philipHPG
    • Johan Hedman
    • mrwatson-gbs
    • Markus Schneider

    Context

    Currently using an empty path in the JSONSetElement function, like this:

     

    $JSON = JSONSetElement( "" ;
    [ "Good" ; "JSON" ; JSONString ];
    [ "" ; $Value ; JSONString ]
    )
    

     

    destroys the existing JSON (and ouputs just the - unprocessed - $Value).

     

    See JSONSetElement with empty path overwrites the whole JSON

     

    Idea

     

    JSONSetElement with an empty path should not just destroy the existing JSON.

    JSONSetElement should handle empty paths in a more useful way:

     

    Use #1: Merging JSONObjects

     

    An empty path should be used to merge JSON objects (at the root), thus

     

    $JSON1 = JSONSetElement( "" ; "a" ; 1 ; JSONNumber )
    $JSON2 = JSONSetElement( "" ; "b" ; 2 ; JSONNumber )
    $JSON3 = JSONSetElement( $JSON1 ; "" ; $JSON2 ; JSONObject ) 
    

     

    should produce

     

         {"a":1,"b":2}

     

    Similarly, with multiple objects:

     

    $JSON4 = JSONSetElement( "" ; "four" ; "IV" ; JSONString )
    $JSON5 = JSONSetElement( $JSON3 ; 
    [ "" ; $JSON1 ; JSONObject ];
    [ "" ; $JSON2 ; JSONObject ];
    [ "" ; $JSON4 ; JSONObject ]
    )
    

     

    should produce

     

         {"a":1,"b":2,"four":"IV"}

     

     

    Use #2: Adding conditional elements

     

    If BOTH path AND Value are empty, the triplet should be ignored, so that you could programme like this:

     

    $JSON5 = JSONSetElement( "" ; 
    [ "Name" ; $Name ; JSONString ];
    [ Case( not IsEmpty( $Option1 ) ; "Option1" ) ; $Option1 ; JSONString ];
    [ Case( $debug ; "Debug.Level" ) ; $debug ; JSONNumber ]
    )
    

     

     

     

     

    Benefits

    • Code is (much) simpler + FileMakery
    • Less Gotchas

     

    Use Cases

    • See above

     

    Sorry - run out of time - tell me what you think!

     

    (Hm ... I think it needs thinking through a bit so the two uses don't clash.)

     

     

    …Struggling for a better FileMaker Universe…

     

    MrWatson