How to convert JSON file to a WSTRING?

I want to use WebClient Function Block from Web_Client_SL library in IIOT to PUT an APPLICATION_JSON. For this goal I need to define the PostValue as a WSTRING and then define a Pointer for it to give the pointer as an input to FB. The first problem which I have is that when I give a json file to a WSTRING variable, the compiler returns an error. So what is the proper way to assign this json string in a variable?

For solving this problem, I used the JSON_Utilities_SL library in IIOT to convert a JSON file to a WSTRING. With JSONFileReader FB, I can make a JSONData object. How we can convert this JSONData object to WSTRING?

There is a function called JSONElementToString. For implementing it we need to extract the JSON elements. So I used the GetElementByIndex method to get the JSON elements. But the problem is that, I do not know the number of indexes for an arbitrary JSON file. What should I do?

Best reply by HmiGuide

I'm not sure, if I understand your description correct. I see  2 differnet use cases:

A: Pass the JSON file content, without changing anything

  1. Open file, Read content STRING/WSTRING, Close file
  2. Send WSTRING via Web_Client_SL library

B: Pass the JSON file content, with changing it

  1. Open file, Read content STRING/WSTRING, Close file
  2. Parse string into JSON object
  3. Edit JSON object
  4. Create WSTRING from JSON object
  5. Send WSTRING via Web_Client_SL library

Remarks:

  • When you do not have to change the content of the JSON object, you do not need the JSON library of CoDeSys.
  • Just use the library CXA_FileAsync. It's a wrapper around the CoDeSys function, which are really hard to use.
  • If the file format is STRING, you have to read it as STRING and convert it afterwards to WSTRING
  • If the file format is WSTRING, you have to read it as WSTRING
  • In my tests around 1 year ago, the CoDeSys JSON library was not very stable. I would use it only for very basic stuff but not for complex JSON's.
View original
4 replies