Jump to content

Convert to JSON string a string from "Byte Array to String"


Recommended Posts

Dear all,

I have to transmit a JSON message with a string of chars to an external REST API (in python or javescript). In LabVIEW I have an array of uint8, converted to a string with "Byte Array to String.vi". This string has the expected length (equal to the length of the uint8 array).

When I convert a cluster with this string in the field "asd" to a JSON string, the resulting string of the field "asd" is 1. different 2. longer. This is true using three different functions to convert (the NI Flatten to json, the package JSONtext and the JKI json serializer).

If i convert the JSON string to a cluster, in LabVIEW, the resulting string in "asd" is again equal to the original one and with the same amout of chars. So everthing is fine, in LabVIEW.

 immagine.png.919e253e9b616cc2227ce838fbfe7653.pngimmagine.png.3ecfe58332720cfc80114ee26ad3d00d.png

But the following code in Python, for example, doesn't return the original string:

import json

orig = r'ýÁ%VÚõÏ}ŠX'
json_string = r'{"test":"ýÃ%VÚõÃ}ÂÅ \u0002X\u001C\u0000d3"}'

convert = json.loads(json_string)
print(convert['test'])
# return ýÃ%VÚõÃ}ÂÅ Xd3

print(convert['test'] == orig)
# return False

Can anyone help me or suggest a different approach?

Thank you,

Marco.

 

Link to comment
1 hour ago, Lior Bilia said:

Why does the python code has 2 different constants? 

Do you mean 'orig' and 'json_string' ?  The secondo one, json_string, is the string that the Python API receive from labview. The labview vi in my previous screenshot is a minimal example, but imagine that the json string created by labview is transmitted to the Python API; the python code is, again, a minimal example where the idea is that 'json_string' is the string received from labview trough the API.

When I try to convert the json string, in python, the string in 'convert['test']' is not equal to `orig`/original string in the labview vi. While, the conversion of the same json string return the same original string if done by LabVIEW.

Link to comment

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.