Skip to content

Instantly share code, notes, and snippets.

@steveAllen0112
Last active May 26, 2018 05:37
Show Gist options
  • Save steveAllen0112/dc332d38a94aad3abde423227ea85593 to your computer and use it in GitHub Desktop.
Save steveAllen0112/dc332d38a94aad3abde423227ea85593 to your computer and use it in GitHub Desktop.
// JSON.ArraySlice ( data ; start ; numberOfValues )
// Orignally done by Steve Allen (GitHub: @steveAllen0112) of Richard Carlton Consulting (RCC) 05/2018
// With some "polishing" suggestions from Michael Wallace (GitHub: @mw777rcc), also of RCC
Case (
/* Check if it's even trying to be an array */
Left ( Trim ( data ) ; 1 ) <> "["
; "? Invalid input; this function only accepts arrays."
/ * Check if it's valid JSON */
; Let ( $check = JSONGetElement ( data ; 0 ) ; Left ( $check ; 1 ) = "?" )
; $check
; /* OK, now we can actually proccess it */
Let ([blank=""
/* turn the array into something we can work with with List management functions already in FM */
; jsonValues = JSONListValues ( data ; "" )
/* now grab what we were actually looking for - the subset of array items we want */
; sliced = MiddleValues ( jsonValues ; start ; numberOfValues )
/* But MiddleValues appends an extra return on the end that we need to nix before turning the list back into JSON. */
; lastReturnStripped = Replace ( sliced ; Length ( sliced ) ; 1 ; "" )
/* now we have to restore this to proper JSON that can be wrapped with the array brackets. */
; putBackAsJSON = "["
& Substitute ( lastReturnStripped
/* we have to separate the objects by commas again for that. */
; [ "¶" ; "," ]
) &
"]"
];
putBackAsJSON
) //end let
) //end case
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment