While ([ ~parts = "" ; ~repetitionIsValid = True ; ~isContainerField = False ; ~isSummaryField = False ; ~fieldCount = ValueCount ( JSONListKeys ( $currentQuery ; "fieldNames" ) ) ; ~j = 0 ] ; ~j < ~fieldCount and ~repetitionIsValid and not ~isContainerField ; [ ~j = ~j + 1 ; // Strict Field name ~selector = JSONGetElement ( $currentQuery ; "fieldNames[" & ( ~j - 1 ) & "]" ) ; // Field Name ~reference = $tableName & "::" & ~selector ; // Field Type ~fieldType = FieldType ( "" ; ~reference ) ; ~dataType = MiddleWords ( ~fieldType ; 2 ; 1 ) ; ~JSONType = Case ( ~dataType = "Text" ; "JSONString" ; "JSONNumber" ) ; // Detect Container Field ~isContainerField = ~dataType = "Container" ; // Validate repeting fields ~numberOfRepetitions = GetAsNumber ( MiddleWords ( ~fieldType ; 4 ; 1 ) ) ; ~hasRepetitionSelector = Position ( ~selector ; "[" ; 1 ; 1 ) > 0 ; ~requestedRepetition = Case ( ~hasRepetitionSelector ; GetAsNumber ( Middle ( ~selector ; Position ( ~selector ; "[" ; 1 ; 1 ) + 1 ; Position ( ~selector ; "]" ; 1 ; 1 ) - Position ( ~selector ; "[" ; 1 ; 1 ) - 1 ) ) ; 1 ) ; ~repetitionIsValid = ~requestedRepetition >= 1 and ~requestedRepetition <= ~numberOfRepetitions ; // JSON Key Path ~open = Position ( ~selector ; "[" ; 1 ; 1 ) ; ~path = Case ( ~open = 0 ; ~selector ; "['" & ~selector & "']" ) ; ~parts = List ( ~parts ; Case ( IsEmpty ( ~parts ) ; "" ; " ; " ) & "[ " & Quote ( ~path ) & " ; GetNthRecord ( " & ~reference & " ; ~i ) ; " & ~JSONType & " ]" ) ] ; Case ( ~repetitionIsValid and not ~isContainerField ; Let ([ ~identityReference = $tableName & "::" & JSONGetElement ( $currentQuery ; "identityField" ) ; // Identity Field Type ~identityFieldType = FieldType ( "" ; ~identityReference ) ; ~identityDataType = MiddleWords ( ~identityFieldType ; 2 ; 1 ) ; ~IdentityJSONType = Case ( ~identityDataType = "Text" ; "JSONString" ; "JSONNumber" ) ; // Gate against offending Field types ~isContainerField = ~identityDataType = "Container" ; ~isSummaryField = MiddleWords ( ~identityFieldType ; 1 ; 1 ) = "Summary" ] ; Case ( not ~isSummaryField and not ~isContainerField ; Let ([ // Build record expression ~recordExpression = Case ( ~fieldCount = 0 ; Quote ( "{}" ) ; "JSONSetElement ( \"{}\" ; " & ~parts & " )" ) ; // Build Retrieval formula expression ~collectorExpression = /* --------------------------------------------------------------------------- Indentation for harvest expression has been reset for readability purposes. We are setting ~collectorExpression here --------------------------------------------------------------------------- */ Case ( ~identityDataType = "Text" ; " While ([ ~identityBody = \"\" ; ~dataBody = \"\" ; ~i = 0 ] ; ~i < " & $iMax & " ; [ ~i = ~i + 1 ; ~identityBody = ~identityBody & \";~v\" & Substitute ( GetNthRecord ( " & ~identityReference & " ; ~i ) ; [ \"~\" ; \"~e\" ] ; [ \";\" ; \"~s\" ] ) ; ~dataBody = List ( ~dataBody ; Case ( IsEmpty ( ~dataBody ) ; \"\" ; \",\" ) & " & ~recordExpression & " ) ] ; \"{ \\\"data\\\": [\" & ~dataBody & \"], \\\"error\\\": 0, \\\"identity\\\":\" & Substitute ( JSONMakeArray ( Middle ( ~identityBody ; 2 ; Length ( ~identityBody ) - 1 ) ; \";\" ; JSONString ) ; [ \"~v\" ; \"\" ] ; [ \"~s\" ; \";\" ] ; [ \"~e\" ; \"~\" ] ) & \"}\" ) " ; // Else if ~identityDataType <> "Text" " While ([ ~identityBody = \"\" ; ~dataBody = \"\" ; ~i = 0 ] ; ~i < " & $iMax & " ; [ ~i = ~i + 1 ; ~identityArray = JSONSetElement ( \"[]\" ; \"[0]\" ; GetNthRecord ( " & ~identityReference & " ; ~i ) ; " & ~IdentityJSONType & " ) ; ~identityBody = List ( ~identityBody ; Case ( IsEmpty ( ~identityBody ) ; \"\" ; \",\" ) & Middle ( ~identityArray ; 2 ; Length ( ~identityArray ) - 2 ) ) ; ~dataBody = List ( ~dataBody ; Case ( IsEmpty ( ~dataBody ) ; \"\" ; \",\" ) & " & ~recordExpression & " ) ] ; \"{ \\\"data\\\": [\" & ~dataBody & \"], \\\"error\\\": 0, \\\"identity\\\": [\" & ~identityBody & \"] }\" ) " ) /* --------------------------------------------------------------------------- Regular identation resumes The parenthesis that follows closes the inner-most Case () evaluation --------------------------------------------------------------------------- */ ] ; Evaluate ( ~collectorExpression ) ) ; False ) ) ; False ) )