Brian Dunning's FileMaker Custom Functions

value.toJsonKeyName ( value )

Given a value, makes it safe for use as a JSON key name by removing invalid characters, and spaces, and replacing them with underscores. Also, if the key name is a reserved word, it will be prefixed with an underscore.

  Be the first to rate this function Log in to vote

Cristos Lianides-Chin   Cristos Lianides-Chin - Show more from this author
Codence
https://www.codence.com

Share on Facebook Share on Twitter

  Sample input:
value.toJsonKeyName ( "My Value" )
  Sample output:
"My_Value"

  Function definition: (Copy & paste into FileMaker's Edit Custom Function window)

You can test with the following data viewer snippet:
```

Let (
[ // Define the test strings
~testStrings = List (
"My Value" ;
" Trim Spaces " ;
"Multi\¶Line" ;
"error" ;
"1startsWithNumber" ;
"Áccentèd" ;
"!@#$%^&*()_+{}|:<>?[];',./" ;
"json" ;
"json_array" ;
"🙂 Emoji" ;
"中文 Chinese" ;
"русский Russian" ;
"العربية Arabic"
)

]

; // Use the While() function to loop through the test strings
While (
[ // Initialize the loop variables
~index = 1
; ~resultsList = ""
]
; ~index ≤ ValueCount ( ~testStrings )
;
[ ~testString = GetValue ( ~testStrings ; ~index )
; ~result = value.toJsonKeyName ( ~testString )
; ~resultsList = List ( ~resultsList ; "Input: " & ~testString & " | Output: " & jsonsetelement ( "{}" ; ~result ; "bar" ; JSONString ) )
; ~index = ~index + 1
]
; ~resultsList
)
)
```

 

Comments

Log in to post comments.

 

Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.

Support this website.

This library has been a free commmunity resource for FileMaker users and developers for 20 years. It receives no funding and has no advertisements. If it has helped you out, I'd really appreciate it if you could contribute whatever you think it's worth: