--TIM140324 16:20 ID = 2252083434564588; VID = 32; TIP = -1    [{"1111":"2252131285821281","2222":"2252131285821282"},{"3333":"2252131285821283"}]  [{"8666":"2252131285821288"}]
--EXECUTE BLOCK MPDRIVER: РАСПАРСИВАНИЕ ПАРАМЕТРОВ В ФОРМАТЕ JSON (МАССИВ ОБЪЕКТОВ) В ОТДЕЛЬНЫЕ ЗАПИСИ (MOTIV 37006)
EXECUTE BLOCK (
  IN_PARAM_JSON TYPE OF COLUMN APP_EVENT.APP_EVENT_PARAM = :IN_PARAM_JSON
)
RETURNS (
  OUT_APP_PARAM_STR     D_STR1024,
  OUT_APP_PARAM_TIP     d_str1024
)
AS
DECLARE VARIABLE FBLOB TYPE OF D_BLOB_STR;
DECLARE VARIABLE FBLOB1 TYPE OF D_BLOB_STR;
DECLARE VARIABLE FBLOB2 TYPE OF D_BLOB_STR;
DECLARE VARIABLE FPR D_PRIZNAK;
DECLARE VARIABLE FINDEX D_INTEGER;
DECLARE VARIABLE FNM D_INTEGER;
DECLARE VARIABLE FNM2 D_INTEGER;
BEGIN
  /*
    TIM140324 16:20
  */
  --EXECUTE BLOCK MPDRIVER: РАСПАРСИВАНИЕ ПАРАМЕТРОВ В ФОРМАТЕ JSON (МАССИВ ОБЪЕКТОВ) В ОТДЕЛЬНЫЕ ЗАПИСИ (MOTIV 37006)
  --ID_EXEC_BLOCKS = 2252083434564588
  --EXEC_BLOCKS.VID = 32 (VLST.ID_VLST = 19104)
  --EXEC_BLOCKS.TIP = -1 (LST.ID_VLST = 19138, LST.LST_INDEX = -1)

  --*********** ВХОДНЫЕ ПАРАМЕТРЫ: ***************
  --IN_PARAM_JSON: СТРОКА ПАРАМЕТРОВ В ФОРМАТЕ JSON (МАССИВ ОБЪЕКТОВ)

  --*********** ВЫХОДНЫЕ ПАРАМЕТРЫ: ***************
  --OUT_APP_PARAM_STR: СТРОКОВОЕ ЗНАЧЕНИЕ ПАРАМЕТРА
  --OUT_APP_PARAM_TIP: ТИП ПАРАМЕТРА

  --*********** ИНИЦИАЛИЗАЦИЯ: ***************
  IN_PARAM_JSON = TRIM(COALESCE(:IN_PARAM_JSON, ''));

  IF (:IN_PARAM_JSON <> '') THEN
  BEGIN
    FPR = 1;
    FINDEX = 1;
    WHILE (:FPR = 1) DO
    BEGIN
      SELECT JSON_GET_ARRAY_ITEM (:IN_PARAM_JSON, :FINDEX, 1)
      FROM RDB$DATABASE
      INTO :FBLOB;
      IF (:FBLOB IS NULL)
      THEN FPR = 0;
      ELSE
      BEGIN
        FBLOB = SUBSTRING(:FBLOB FROM 2 FOR CHAR_LENGTH(:FBLOB) - 2);
        FNM2 = 1;
        FOR SELECT OUT_STRING_ONE
        FROM P_GEN_LIST_STRING_BLOB (:FBLOB, '","')
        INTO :FBLOB1 DO
        BEGIN
          FNM = 1;
          IF (:FNM2 = 1)
          THEN FBLOB1 = SUBSTRING(:FBLOB1 FROM 2 FOR CHAR_LENGTH(:FBLOB1));
          ELSE FBLOB1 = SUBSTRING(:FBLOB1 FROM 1 FOR CHAR_LENGTH(:FBLOB1) - 1);

          FOR SELECT TRIM(COALESCE(OUT_STRING_ONE, ''))
          FROM P_GEN_LIST_STRING_BLOB (:FBLOB1, '":"')
          INTO :FBLOB2 DO
          BEGIN
            IF (:FNM = 1) THEN
            OUT_APP_PARAM_TIP = CAST(:FBLOB2 AS d_str1024);
            ELSE
            IF (:FNM = 2) THEN
            BEGIN
              IF (POSITION('","', :FBLOB) = 0)
              THEN OUT_APP_PARAM_STR = SUBSTRING(:FBLOB2 FROM 1 FOR CHAR_LENGTH(:FBLOB2) - 1);
              ELSE OUT_APP_PARAM_STR = :FBLOB2;
--              OUT_APP_PARAM_STR = :FBLOB2;
            END
            FNM = :FNM + 1;
          END
          FNM2 = :FNM2 + 1;
          SUSPEND;
        END
        FINDEX = :FINDEX + 1;

      END
    END
  END
END