(no subject)

Page 1.04 of 2.44
<< < 1 2 (Page 2 of 2)
  • maxkemmeren - 2024-05-02

    I Found my thinking mistake. You made it that you can onlyh parse from and to a struct of the same instance name.

    In my example I parse a writing struct and than parse it back into a reading struct of the same type. But as the writing and reading struct have different instance names this match all levels does not become true making the reading struct empty.

    So parsing to and from 1 struct instance will work.

     
    • tvm - 2024-05-02

      Correct. In an actual use case I think the compose and parse would happen on different machines.

       
  • Sen_ - 2024-07-04

    Hi,
    I am using this library. but i am facing some issue. i dont know if this is how the library is supposed to work!
    I am parsing from a JSON string to a JSON struct and the JSON struct has an array in one of its variable. and the array index starts from 0. but then, in the 0 th index of the array i am not getting the expected value. rather the values start from 1st array index.

    Is this the actual working of the library?
    or am i missing some configuration?

     
    • tvm - 2025-06-25

      This should be fixed in the new version 1.0.18.0. Previous to that version, only 1-based arrays were supported.

       
  • risele - 2025-06-01

    Hi,
    I created a FB that read JSON file and convert it to the normal DUTs with this library.
    May be usefull for somebody:
    https://github.com/Risele/CodesysJSONFile/
    There are project at Win3 Virtual controller + exported code (normal .export and xml)

    Also I made some notes about library limitations I found. Leaving here for others.

    Naming

    The names in JSON and in ST-based JSONVARs should match exactly. For JSON fields of

    {"field":"value",
     "otherField":3.14
    }
    

    ST-struct should be:

    TYPE JSONST
    STRUCT
        field  :        JSONVAR;
        otherField:     JSONVAR;
    END_STRUCT
    END_TYPE
    

    Arrays

    The Array could not be topmost structure:

    [
    {"obj": "value"},
    {"obj": "value"}
    ]
    

    This is NOT valid.

    The topmost array should be named:

    {"myArr":
    [
    {"obj": "value"},
    {"obj": "value"}
    ]}
    

    The corresponding ST structure is

    TYPE JSONST
    STRUCT
        obj  :          JSONVAR;
    END_STRUCT
    END_TYPE
    

    And the name of the array in the ST code should match the JSON exactly:

    VAR
      myArr: ARRAY[1..10] OF JSONST;
    END_VAR
    

    UTF8

    Does not work with non-english UTF8 symbols (or I failed to do this).

     
    • tvm - 2025-06-25

      This should be fixed in the new version 1.0.18.0. I did some restructuring to fix some issues with arrays. Only thing I haven't done yet is multi-dimensional arrays.

       
      👍
      1
  • youness - 2025-12-09

    Hello Everyone ,

    • Bugs detected during testing with PROJSON 1.0.16.0:
      A new bug has been discovered in the PROJSON library, an incorrect JSON is produced when the message to be composed is a structure array, and one of its attributes is another structure:

    Example that does not work :

    testLevel1: JSONVAR;
    house.rooms[1.. N].window.Var1
    

    Example that works :
    testLevel1: JSONVAR;
    house.rooms[1..N].Var1

    Example that works :
    house.room1.window.Var1

    In our testing phase, we took into account previously identified bugs, in particular:
    - The prohibition of having a single array in a JSON structure; it must be preceded by a JSON variable.
    - The array index must start at 1.

    Solution: We adopted the latter solution, defining each room flatly to avoid having a structure array whose attributes
    include another structure:
    house.room1.window.koeffCorrecteur
    house.room2.window.koeffCorrecteur
    ...
    house.room7.window.koeffCorrecteur

     
    👍
    1
  • lucawu - 2025-12-11

    Hello everyone, I found a bug in Version 1.0.18.0.
    I have a structure:

    TYPE ST_JSON_TEST :
    STRUCT
    testList : ARRAY[0..1] OF JSONVAR;
    END_STRUCT
    END_TYPE

    It is declared as follows:
    stJsonTest : ST_JSON_TEST;
    stJsonTest.testList[0].CharString := 'used';

    stJsonTest.testList[1] is null;

    I called the method "STRUCT_TO_JSON" with the input parameter "IgnoreNull := true".
    After that, the string I got is:
    '{"stJsonTest":{"testList":["used"}]}'

    You can see that the position of ']' is incorrect. Does anyone know how to fix this? Thank you for any advice!

     

    Last edit: lucawu 2025-12-11
  • risele - 2026-02-11

    Hi,
    it seems I have fixed issues with arrays of structs.
    Also added option for rootless JSON exporting/uploading

    And multidimensional arrays!

    https://github.com/Risele/PRO_JSON/

    Hope it will work for you: I made brief tests at my structures only.

    PS: made with help of AI agent, found it's quite good if you give him project/files exported as PLCOpenXML.

     
    👍
    1
    • lucawu - 6 days ago

      Thank you for your fixes. I tried version 1.0.21.0 of your library, and the Rootless feature for STRUCT_TO_JSON works great.
      However, I found that it still doesn't correctly output the closing brackets when serializing an array of structures. Have you tested this kind of structure:
      TYPE ST_PLANT :
      STRUCT
      NAME : JSONVAR;
      QUANTITY : JSONVAR;
      END_STRUCT
      END_TYPE

      TYPE ST_ANIMAL :
      STRUCT
      NAME : JSONVAR;
      AGE : JSONVAR;
      END_STRUCT
      END_TYPE

      TYPE ST_BIOTA :
      STRUCT
      PLANT : ARRAY[0..3] OF ST_PLANT;
      ANIMAL : ARRAY[0..3] OF ST_ANIMAL;
      END_STRUCT
      END_TYPE
      When using STRUCT_TO_JSON with my ST_BIOTA structure, regardless of whether my Rootless setting is TRUE or FALSE, the generated string is always missing the final ']' and the preceding '}'. I would be very grateful if you could clarify this issue for me.
      Regardless, thank you again for your contribution, respect!

       
      • risele - 5 days ago

        Just tested with your structs and seems JSON is perfectly valid.
        With root:

        {"biota":{"PLANT":[{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null}],"ANIMAL":[{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null}]}}
        

        Rootless:

        {"PLANT":[{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null}],"ANIMAL":[{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null}]}
        

        The code I used:

        PROGRAM PLC_PRG
        VAR
        biota: ST_BIOTA;
        converter,converter1: STRUCT_TO_JSON;
        xRun,xRun1:BOOL:=TRUE;
            JSONString:         STRING(1000);
            JSONStringRootless:         STRING(1000);
        
        
        END_VAR
        
        IF xRun THEN
            IF converter.Done OR converter.Error THEN
                xRun:=FALSE;
            END_IF
        END_IF
        IF xRun1 THEN
            IF converter1.Done OR converter1.Error THEN
                xRun1:=FALSE;
            END_IF
        END_IF
        converter(JSONVars:=ADR(biota),NumberOfVars:=SIZEOF(biota)/SIZEOF(JSONVAR),
            JSONString:= ADR(JSONString), JSONStringSize:= SIZEOF(JSONString),
            Execute:=xRun,Rootless:=FALSE);
        converter1(JSONVars:=ADR(biota),NumberOfVars:=SIZEOF(biota)/SIZEOF(JSONVAR),
            JSONString:= ADR(JSONStringRootless), JSONStringSize:= SIZEOF(JSONStringRootless),
            Execute:=xRun1,Rootless:=TRUE);
        

        UPD:
        for top-level described array it also provides valid output, BUT only with ROOTLESS=FALSE option. It's not an error, I suppose, since nameless array could not exists in JSON.

        For

        biota: ARRAY [1..3] OF ST_BIOTA;
        

        output is

        {"biota":[{"PLANT":[{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null}],"ANIMAL":[{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null}]},{"PLANT":[{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null}],"ANIMAL":[{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null}]},{"PLANT":[{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null},{"NAME":null,"QUANTITY":null}],"ANIMAL":[{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null},{"NAME":null,"AGE":null}]}]}
        
         

        Last edit: risele 4 days ago
<< < 1 2 (Page 2 of 2)

Log in to post a comment.