JSONFromFields ( json ; fieldNameList ; excludeFieldNameList )
Builds a JSON Object {} or Array [] with each field's value as the correct type of JSON element
Average rating: 5.0 (1 vote) Log in to vote
Jonathan Mickelson - Show more from this author |
JSONFromFieldList ( "{}" ; "FirstNam ¶people:::Age" ; "" ) =
JSONFromFieldList ( "[]" ; "LastName¶FirstName¶people:::Age" ; "people::FirstName" ) =
JSONFromFieldList ( "[]" ; "people:::Age & ¶ & FirstName" ; "" ) =
= {"FirstName":"Jane","Age":24}
= ["Smith",24]
= [24,"Jane"]
Function definition: (Copy & paste into FileMaker's Edit Custom Function window)
Description: A Recursive Function that uses the 'json' parameter to determine what
JSON Object or Array should be built. Then builds JSON Elements in the JSON root
for each fieldName in the return delimited value list parameter 'fieldNameList'.
Use Cases:
1) Dynamically build JSON from fields on the Current Layout.
2) A Calc Field that aggregates JSON for records from a table, so the data can be accessed from any
context for that record, scraping a system layout, with a Unstored Calculation Field Calc,
like the below:
Let (
[
excludeMe = "z_Record_JSON_ct" ; //static field name so doesn't reference itself
layoutForFields = "z_ImportantFields_JSON" ; // Use Get ( LayoutName ) for dynamic
fieldList = FieldNames ( Get ( FileName ) ; layoutForFields ) ;
json = JSONFromFields ( "{}" ; fieldList ; excludeMe )
] ;
JSONFormatElements ( json )
)
Examples:
NOTE: fieldNames should be generated with GetFieldName ( field ) rather than hardcoded as in examples below
- JSONFromFieldList ( "" ; "FirstName¶people:::Age" ; "" ) = {"FirstName":"Jane","Age":24}
- JSONFromFieldList ( "{}" ; "FirstNam ¶people:::Age" ; "" ) = {"FirstName":"Jane","Age":24}
- JSONFromFieldList ( "[]" ; "LastName¶FirstName¶people:::Age" ; "FirstName" ) = ["Smith",24]
- JSONFromFieldList ( "[]" ; "people:::Age & ¶ & FirstName" ; "" ) = [24,"Jane"]
Parameters:
json - a JSON Object {} or Array [], if already exists, or can define a new one:
- "" value = a JSON Object {} with name value pairs. Same as "{}"
- "{}" value = a JSON Object {} with name value pairs.
- "[]" value = a JSON Array [] with indexed values in the order of the fields provided.
fieldNameList - List of return delimited fieldNames, fully qualified or simple (local table format),
that you want to generate into a JSON Object or Array.
excludeFieldNameList - List of return delimited fieldNames, fully qualified or simple (local table format),
that you want excluded from the results. This is good if you use layout scraping to
dynamically generate field lists, and do not want certain sensitive fields included.
NOTE: IF YOU USE THIS FUNCTION TO DISPlAY JSON FROM CURRENT LAYOUT FIELDS
YOU SHOULD ADD THE FIELD TO THE EXCLUDE LIST TO NOT RUN AFOUL OF
FIELD/CALC RECURSION, WHERE THE FIELD ATTEMPTS TO DISPLAY ITSELF
WITH ITSELF AS AN JSON ATTRIBUTE.
Function Result: a JSON Object {} or Array [] with each attributes being the fieldName or index ordered
by the order provided, and the data from the appropriate field.
Comments
Jonathan Mickelson May 7, 2019 |
||
I updated the function and changed it to be called "JSONfromFields" | ||
Note: these functions are not guaranteed or supported by BrianDunning.com. Please contact the individual developer with any questions or problems.