JSONBuilder (FB)ΒΆ

FUNCTION_BLOCK JSONBuilder

The JSONBuilder is a helper FB for building JSON structures. It simplifies the setting of key-value pairs by setting the key and value in one call and using the value of type ANY.

The structure is built using index fields. The root object has the index -1. All methods return the next index. The input diParentIndex is used to set the index of the parent element.

Example:

JSON String to create:

{
  "Key1": "Value1",
  "Key2":
    {
      "Key3": 1234
    }
}
VAR
  factory : JSON.JSONDataFactory;
  eDataFactoryError : FBF.ERROR;
  pJsonData : POINTER TO JSON.JSONData := factory.Create(eError => eDataFactoryError);
  fb_JBuilder : JSON.JSONBuilder;
  wsValue : WSTRING;
  diRootIndex, diObject1Index : DINT;
  iValue : INT;
  jsonArrayWriter : JSON.JSONByteArrayWriter;
  wsJsonData : WSTRING(1000);
  xFirst : BOOL := TRUE;
END_VAR

IF xFirst THEN
  fb_JBuilder(pJsonData := pJsonData, diRootObj => diRootIndex);
  wsValue := "Value1";
  fb_JBuilder.SetKeyWithValue("Key1", wsValue, diParentIndex := diRootIndex);
  diObject1Index := fb_JBuilder.SetKeyWithObject("Key2", diParentIndex := diRootIndex);
  iValue := 1234;
  fb_JBuilder.SetKeyWithValue("Key3", iValue, diParentIndex := diObject1Index);
  xFirst := FALSE;
END_IF

jsonArrayWriter(xExecute := TRUE, pwData := ADR(wsJsonData), udiSize := SIZEOF(wsJsonData), jsonData := pJsonData^, xAsyncMode := FALSE);
InOut:

Scope

Name

Type

Input

pJsonData

POINTER TO JSONData

Output

diRootObj

DINT

Methods:

Structure: