AnsweredAssumed Answered

JSONSetElement with empty path overwrites the whole JSON

Question asked by mrwatson-gbs on May 8, 2018
Latest reply on Jun 27, 2018 by bigtom

System

FileMaker 16, but probably all

Mac, but probably all

 

Issue

Using an empty path in a JSONSetElement function causes the whole JSON expression to be overwritten.

 

Steps to reproduce

Type the following function into the data viewer:

 

JSONSetElement ( "{}" ;
["abc"  ; 123  ; JSONnumber ];
[""     ; ""   ; JSONString ]
)

 

Expected Result

I would expect ["" ; "" ; … ] to be ignored, so that the expression is equivalent to

 

JSONSetElement ( "{}" ;
["abc"  ; 123  ; JSONNumber ]
)

 

giving the expected result

 

{"abc":123}

 

Actual Result

Empty string

 

Similarly:

 

JSONSetElement ( "{}" ;
["abc"  ; 123  ; JSONNumber ];
[""  ; "oh dear"  ; JSONString ]
)

 

gives the unexpected result "oh dear"

 

The current behaviour is unexpected, destructive and not useful in any way I can see. It can thus fairly be considered an issue...

 

…but what should be expected here?

 

I would suggest that where the path is empty the triplet should be ignored - much like the List function ignores empty elements…

 

…Indeed it was whilst updating an old List based (human-readable) text output (containing conditional Case( … ; … ) content ) into a (human + machine-readable) JSON output that I stumbled on this ...er... let's call it a 'behaviour unexpected gaffe' ;-)

 

This would make it possible to construct nodes conditionally in a single JSONSetElement function:

 

JSONSetElement ( "{}" ;
["script"                           ; $script  ; JSONString ];
["result"                           ; $result  ; JSONString ];
[If( $error  ≠ 0   ; "error.num" )  ; $error   ; JSONNumber ];
[If( $errMsg ≠ ""  ; "error.msg" )  ; $errMsg  ; JSONString ]
)

 

This expression

  • would always output the script and result nodes,
  • but the error.num node would only be output when there is a non zero error
  • and the error.msg node would only be output when there is a non empty error message.

 

benefits:

=> update old List( … ) constructs easily to JSON constructs

=> compact and readable code

=> no unexpected loss of JSON data

=> less time debugging

=> greater productivity

=> easier and faster uptake of JSON technology

 

Workaround

Construct the JSON in a more complex way using multiple JSONSetElement commands to only insert nodes when they exist.

 

But the code is much bigger and more complex (needs multiple set fields or a let , ….

Outcomes