Brian Dunning's FileMaker Custom Functions

ArrayReformat ( valueList ; quoteStyle ; braceStyle ; newSeparator ; oldSeparator )

Convert/transform lists to JSON, quoted lists, or other name value pairs

  Average rating: 4.3 (3 votes) Log in to vote

Julio Toledo   Julio Toledo - Show more from this author
Automation USA LLC
https://www.automationusa.net

Share on Facebook Share on Twitter

  Sample input:
ArrayReformat ( "item1¶item2¶item3¶item4" ; "'" ; "" ; "" ; "" )

ArrayReformat ( "item1¶item2¶item3¶item4" ; "\"" ; "{}" ; "" ; "" )

ArrayReformat ( "Label-A¶Label-B¶Label-C¶Label-D" ; "«»" ; "[]" ; "; "; "" )

ArrayReformat ( "Label-1,Label-2,Label-3,Label-4" ; "\"" ; "null" ; "¶"; "," )
  Sample output:
('item1','item2','item3','item4')

{"item1","item2","item3","item4"}

[«Label-A»; «Label-B»; «Label-C»; «Label-D»]

"Label-1"¶"Label-2"¶"Label-3"¶"Label-4"

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

ArrayReformat can be used to transform any list (values separated by any separator [returns, commas, strings, etc.) into any other syntax string using new punctuation and value separators.

Use cases include converting CSV to JSON values, creating quoted lists for SQL "IN" queries as well as to create properly formatted JSON by combining name lists and value lists using a recursive function

Examples:
ArrayReformat ( "Label1,Label2,Label3,Label4" ; "\"" ; "null" ; " : " ; "," ) returns —-> "Label1" : "Label2" : "Label3" : "Label4"

ArrayReformat ( "Label1¶Label2¶Label3¶Label4" ; "«»" ; "" ; "; " ; "" ) returns —-> («Label1»; «Label2»; «Label3»; «Label4»)

ArrayReformat ( "item1¶item2¶item3¶item4" ; "'" ; "" ; "" ; "" ) returns —-> ('item1','item2','item3','item4')

Create quoted lists for executing SQL queries such as "SELECT firstname,lastname,city,state FROM myContacts WHERE state IN ('MI','IN','OH')":
ArrayReformat ( "MI¶IN¶OH" ; "'" ; "" ; "" ; "" ) returns —-> ('MI','IN','OH')

Create well formed JSON and other name-value pairs by using in conjunction with a merge array function ( such as "ArrayCombine" http://www.briandunning.com/cf/2037). For example:

Let ( [

list1 = ArrayReformat ( "Label1¶Label2¶Label3¶Label4" ; "\"" ; "null" ; "¶" ; "" ) ;
list2 = ArrayReformat ( "fantastic!¶...but what about¶a syntax for adding¶an element to the END of an array?" ; "\"" ; "null" ; "¶"; "" )
] ;

//calculation
ArrayReformat ( ArrayCombine ( list1 ; list2 ; 1 ; " : " ) ; "{}" ; "[]" ; " , " ; "" )
)

Returns the following JSON —> [{"Label1" : "fantastic!"} , {"Label2" : "...but what about"} , {"Label3" : "a syntax for adding"} , {"Label4" : "an element to the END of an array?"}]

NOTES:
To opt out of the enclosing "braceStyle" punctuation, Use "null" parameter value

RELEASE:
2017-04-27 - Initial release by Julio Toledo, Automation USA, LLC
2017-05-05 - Modifications by Julio Toledo, Automation USA, LLC:
* renamed function from "ReformatArray" to "ArrayReformat"
* added "oldSeparator" optional parameter, which provides the ability to substitute any existing value separator for any other separator (defaults to return "¶")

 

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: