Avatar
Please consider registering
guest
sp_LogInOut Log Insp_Registration Register
Register | Lost password?
Advanced Search
Forum Scope


Match



Forum Options



Minimum search word length is 3 characters - maximum search word length is 84 characters
sp_Feed Topic RSSsp_TopicIcon
OPC UA JSON DataEncoding for OPC UA Built-In types
November 26, 2019
16:38, EET
Avatar
gerstale
Member
Members
Forum Posts: 12
Member Since:
November 26, 2019
sp_UserOfflineSmall Offline

Hello,

could someone please provide examples how to encode OPC UA Built-In types to JSON for further processing? Assuming that it should be possible, as it specified in OPC 10000-6, OPC Unified Architectur, Part 6: Mappings.

Thank you in advance

November 27, 2019
10:43, EET
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1052
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

We do not support that yet. It is a relatively new thing, as it was only added in 1.04.

We will eventually release a “JsonEncoder” as it is needed for PubSub support that we are currently working on.

May 9, 2025
15:13, EEST
Avatar
msaala
Member
Members
Forum Posts: 5
Member Since:
November 16, 2015
sp_UserOfflineSmall Offline

Hello,
I’d like to come back to this very old topic because I think a lot has happened since then. I’m using version 5.2 of the Prosys OPC UA SDK, which now includes the announced JsonEncoder/JsonDecoder. I now have a similar task: the value from a node should be read and converted to JSON. In my case, the value is of the data type Structure or Structure[]. The best solution would be as generic as possible, one that can convert virtually any value from a node to JSON. I think this is a question you’re familiar with. Unfortunately, I haven’t found any documentation on JsonEncoder other than javadoc, but it’s not very comprehensive. Am I even on the right track with JsonEncoder? Do you have any other documentation/examples that I just haven’t found?

Thank you for any advice
Michael

May 12, 2025
13:15, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1052
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

Hi,

Can you explain your use-case in full? The state of JSON is currently a bit of a mess.

There isn’t a “single JSON format” in OPC UA. That was the case already in 1.04. However, currently there are 2 new modes being added in the specification (plus extra PubSub rules on top of those) and the old format is being deprecated. We do not yet have a release nor a beta with the new format. It will probably take some time before we support them, as the spec isn’t finalized either, currently (as of 1.05.04) the https://reference.opcfoundatio…..5/docs/5.4 does say:
“Editors Note: The JSON encoding was changed very late in the specification release cycle with limited implementation to validate the design. Subsequent implementations and interoperability testing may result in changes to the JSON encoding in future versions of this specification.”

Regarding the com.prosysopc.ua.stack.encoding.json.JsonEncoder, generally speaking classes that are in com.prosysopc.ua.stack.* packages can be thought to be internal implementation and shouldn’t be used unless they are part of the API of other com.prosysopc.ua.* package classes. That being said sometimes (like in this case) a thing can only be done via them, but generally speaking they aren’t that well documented and can change more freely.

Probably the JsonEncoder design should also be better as well in the future, but currently the following should help:

EncoderContext ctx = obtain from UaClient/UaServer .getEncoderContext;

ByteArrayOutputStream data = new ByteArrayOutputStream();
JsonEncoder enc = new JsonEncoder(ctx, new OutputStreamWriter(data, StandardCharsets.UTF_8));

enc.put("TestField", new Variant(Integer.valueOf(42)), UaIds.BaseDataType, 0);
enc.close();

String dataStr = new String(data.toByteArray(), StandardCharsets.UTF_8);
System.out.println(dataStr);

You should see {“TestField”:{“Type”:6,”Body”:42}} in the output.

You can also use some of the other public API of JsonEncoder. However, in a generic case you should have a DataValue and then the Variant within it. Skipping a bit details here, but using that Variant, then UaIds.BaseDataType to get the UaNodeId related to BaseDataType and finally 0 as the dimensions (i.e. scalar, note that the scalar Variant can contain arrays) it should work for any Variant (except e.g. we are aware that an array of Structures within a Variant doesn’t work properly currently).

May 12, 2025
18:29, EEST
Avatar
msaala
Member
Members
Forum Posts: 5
Member Since:
November 16, 2015
sp_UserOfflineSmall Offline

Hello Bjarne,
Thank you for your quick reply. Our use case, in short: Read the value of a node (or react to a value change via a subscription) and convert it to JSON. Ideally, the conversion should be performed according to a clear specification. The node’s data type can be a built-in standard type, a structure, or an array of these types.
In the implementation, I ended up with code similar to that in your snippet. However, there were problems with arrays of structures, and the method I used, JsonEncoder.putStructure(String, Structure), is deprecated. So I wasn’t sure if I was on the right track. If I understand you correctly, there is a well-known problem with arrays of structures. However, I can replace the deprecated method, as shown in your snippet. Alternatively, I could try to implement a conversion myself based on the StructureSpecification.

May 15, 2025
14:49, EEST
Avatar
Bjarne Boström
Moderator
Moderators
Forum Posts: 1052
Member Since:
April 3, 2012
sp_UserOfflineSmall Offline

I meant more like what you do with the JSON once you would have it (if possible to explain). For example, do you need to be able to parse it back, do you use only it for your app or do you need to send it etc.? Also if sending does the receiver need to “know OPC UA” and do they have access to the address space via OPC UA client-server?

Forum Timezone: Europe/Helsinki
Most Users Ever Online: 1919
Currently Online:
Guest(s) 28
Currently Browsing this Page:
1 Guest(s)
Top Posters:
Heikki Tahvanainen: 402
hbrackel: 144
rocket science: 99
pramanj: 86
Francesco Zambon: 83
Ibrahim: 78
Sabari: 62
kapsl: 57
gjevremovic: 49
Xavier: 43
Member Stats:
Guest Posters: 0
Members: 769
Moderators: 7
Admins: 1
Forum Stats:
Groups: 3
Forums: 15
Topics: 1556
Posts: 6563
Newest Members:
odellflournoy9, Robertlom, kimhastings59, fentuy, KBrendel, GarryTerse, Shawnreify, Albertfug, madisonkilburn0, renafitchett0
Moderators: Jouni Aro: 1039, Pyry: 1, Petri: 1, Bjarne Boström: 1052, Jimmy Ni: 26, Matti Siponen: 359, Lusetti: 0
Administrators: admin: 1