Concept

This snippet shows how to write CFL to convert an INI file formatted string to a JSON formatted string.

DECLARE INIFile :=
'Description=Friday
Quantity=5';
DECLARE JSON := "";
DECLARE line := "";
DECLARE propvalue := "";
DECLARE prop : = "";
DECLARE p := 0;
DECLARE lineend := 0;
WHILE (LEN(INIFile)> 0) DO
  lineend := pos(INIFile, CHAR(13),1)-1;
  IF (lineend0) THEN
    p := pos(line, "=");
    IF (p = 0) THEN
      JSON := JSON + '"' + line + '" : "", '
    ELSE
      propvalue := TRIM(MID(line, p+1,9999));
      JSON := JSON
              + IF (LEN(JSON)>1) THEN ", " ELSE "" ENDIF
              + '"' + TRIM(LEFT(line, p-1)) + '" : '
              + IF ISNumber(propvalue) THEN propvalue ELSE '"'+propvalue+'"' ENDIF;
    ENDIF;
  ENDIF;
  INIFile := TRIM(MID(INIFile, lineend+1, 9999));
ENDWHILE;
JSON := "{ "+JSON+" } "

Contributor: Cyrious Software

Date: 2017-05

Version: Control 4+

You could leave a comment if you were logged in.