-1

How do I iterate OutputRoot.JSON.Data and remove empty string fields

Input

{
firstName : 'test,
lastName : ''
}

Result

{
firstName : 'test'
}
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
lars1595
  • 886
  • 3
  • 12
  • 27
  • Could you post a actual json with repeatable elements to understand actually over what would you iterate. The example you mentioned doesn't have anything to iterate over. You can just parse the data as JSON and then write some esql code to check whether lastname is empty. If emplty make the OutputRoot not spit out that element. – Rohan Feb 21 '20 at 21:06
  • Please post a minimal reproducible example of your problem. Include the code that you have written, and a description of the problems that you are facing when you run it. – kimbert Feb 24 '20 at 11:19

2 Answers2

1

Please, take a look code below:

    CREATE PROCEDURE removeEmpty (IN inData REFERENCE, INOUT outData REFERENCE) BEGIN
        DECLARE n INTEGER CARDINALITY(inData.*:*[]);
        DECLARE i INTEGER 0;
        DECLARE Ref REFERENCE TO outData;

        WHILE i < n DO
            SET i = i + 1;

            DECLARE fieldTp INTEGER FIELDTYPE(inData.*:*[i]);
            -- 50331648 is NameValue type
            IF fieldTp = 50331648 THEN
                IF FIELDVALUE(inData.*:*[i]) IS NOT NULL AND LENGTH(CAST(FIELDVALUE(inData.*:*[i]) AS CHARACTER)) > 0 THEN
                    CREATE LASTCHILD OF Ref TYPE FIELDTYPE(inData.*:*[i]) NAME FIELDNAME(inData.*:*[i]) VALUE FIELDVALUE(inData.*:*[i]);
                END IF;
            ELSE
                CREATE LASTCHILD OF Ref TYPE FIELDTYPE(inData.*:*[i]) NAME FIELDNAME(inData.*:*[i]);
                CALL removeEmpty(inData.*:*[i], Ref.*[<]);
                MOVE Ref TO outData;
            END IF;
        END WHILE;

        MOVE outData TO Ref;
    END;
0

This how i solved it

WHILE LASTMOVE(ref) DO 
    DECLARE count INT 0;
    DECLARE total INT  CARDINALITY(ref.*:*[]);
    WHILE count < total DO 
        SET count  = count +1; 
        IF NOT TRIM(FIELDVALUE(ref.*:*[count])) = '' THEN
            SET OutputRoot.JSON.Data.{FIELDNAME(ref.*:*[count])}  = TRIM(FIELDVALUE(ref.*:*[count]));
        END IF; 

    END WHILE;
    MOVE ref NEXTSIBLING REPEAT NAME;
END WHILE;
lars1595
  • 886
  • 3
  • 12
  • 27