Welcome to our new forum
All users of the legacy CODESYS Forums, please create a new account at account.codesys.com. But make sure to use the same E-Mail address as in the old Forum. Then your posts will be matched. Close

JSON parsing and composing library:

oooo
2022-12-01
2024-02-21
  • oooo - 2022-12-01

    ParseJSON failed with Version 1.0.0.13/14 & codesys v3.5 sp17/sp18, the NewJSONObj get no value.


    Talk Topic about project #pro-json

     
  • tvm - 2022-12-01

    If you post an example of the JSON string I can take a look at it.

     
  • tvm - 2022-12-03

    ok yeah, that's not actually a great example. I'll make some better ones.

    The resulting JSON string from ComposeJSON looks like this:

    {"LocalJSONObj":{"var1":true,"var2":34.8756,"var3":"teststring","obj":{"var4":22,"var5":false,"var6":"qwer","var7":[123.234,55.46001,985]},"arrayobj":[1,2,3]}}
    

    It's created an object starting at the first level "LocalJSONObj" variable. Therefore, it doesn't match the "NewJSONObj" variable in the parse

    There's two ways to fix this:
    1.Use a different string on the parse

    NewJSONStr:         STRING(1000):= '{"NewJSONObj":{"var1":true,"var2":34.8756,"var3":"teststring","obj":{"var4":22,"var5":false,"var6":"qwer","var7":[123.234,55.46001,985]},"arrayobj":[1,2,3]}}';
    
    ParseJSON(
        JSONString:= ADR(NewJSONStr), 
        JSONStringSize:= SIZEOF(NewJSONStr),
        JSONVars:= ADR(NewJSONObj),
        NumberOfVars:= SIZEOF(NewJSONObj) / SIZEOF(JSONVAR)
    );
    

    OR

    2.Set the "MaxLevels" input on the compose to 1. Then you can parse it into any variable that has the same structure.

    ComposeJSON(
        JSONString:= ADR(JSONString), 
        JSONStringSize:= SIZEOF(JSONString),
        MaxLevel:= 1,
        JSONVars:= ADR(LocalJSONObj),
        NumberOfVars:= SIZEOF(LocalJSONObj) / SIZEOF(JSONVAR)
    );
    

    This will prevent creating an object for the "LocalJSONObj" variable, so your resulting string will look like this:

    {"var1":true,"var2":34.8756,"var3":"teststring","obj":{"var4":22,"var5":false,"var6":"qwer","var7":[123.234,55.46001,985]},"arrayobj":[1,2,3]}
    
     
    πŸ‘
    1

    Last edit: tvm 2022-12-03
    • oooo - 2022-12-04

      Got it. Thank you tvm.

       

Log in to post a comment.