JSONByteArrayWriter problem?

ryusoup
2023-10-04
2026-06-14
  • ryusoup - 2023-10-04

    Hello,
    I am trying to implement a FB to make JSON formatted WSTRING type message using JSON Utilities 1.9.0.0 included in the IIoT library.

    In my case, I want to set some objects or arrays and set values to them later.
    But when I wrote as the sample below,

    // init builder
    builder(pJsonData:=pJsonData, diRootObj=>diRootObj);
    
    // set the 1st key to root
    diKey1 := builder.SetKeyWithArray(wsKey:="key1", diParentIndex:=diRootObj, eError=>eError);
    
    // set a value to the 1st key
    wsValue:="value01";
    builder.SetValue(Value:=wsValue, diParentIndex:=diKey1, eError=>eError);
    
    // set the 2nd key to root
    diKey2 := builder.SetKeyWithArray(wsKey:="key2", diParentIndex:=diRootObj, eError=>eError);
    
    // set a value to the 2nd key
    wsValue:="value02";
    builder.SetValue(Value:=wsValue, diParentIndex:=diKey2, eError=>eError);
    
    // set a value to the 1st key again
    wsValue:="value03";
    builder.SetValue(Value:=wsValue, diParentIndex:=diKey1, eError=>eError);
    
    // set a value to the 2nd key again
    wsValue:="value04";
    builder.SetValue(Value:=wsValue, diParentIndex:=diKey2, eError=>eError);
    
    // write build result
    writer(xExecute:=TRUE, pwData:=ADR(wsResult), udiSize:=SIZEOF(wsResult), xAsyncMode:=FALSE, jsonData:=pJsonData^);
    

    the result was:

    {
       "key1": 
         ["value01"],
       "key2": 
         ["value02", "value03", "value04"]
    }
    

    while my expection was:

    {
      "key1": 
        ["value01", "value03"],
      "key2": 
        ["value02", "value04"]
    }
    

    Inspecting pJsonData^, JSONData itself seems to be ok, so I believe the issue is something caused by the writer FB.
    Does anyone know how to fix it?

    Thanks,

     
  • brouwyka - 2026-06-08

    Hey there! I am currently running into the exact same issue.
    The JsonData object has all inexes & parent indexes in the way I expect, everything is correctly "linked".
    It purely seems that the writer is incorrectly parsing said JsonData, not based on parent & child index, but on something else that breaks it and puts things at the root, instead of the specified parent index.

    Did you ever manage to find a solution, or another way of achieving your JSON composing?

    I'm currently working on Codesys V3.5 SP19 Patch 7 with IIOT Libraries SL 1.13.0.0, specifically JSON Utilities SL 1.13.0.0 (to send requests to APIs with JSON bodies using Web Client SL 1.13.0.0).

     

Log in to post a comment.