LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

JSON File Replaced with Null Characters When Updated in LabVIEW

I am encountering an issue where a previously existing JSON file is sometimes replaced with null characters of a certain length when it is updated from LabVIEW. I am using the Flatten to JSON String.vi to convert a LabVIEW cluster to a JSON string and save it using the Write to Text File function.

From my observations, the only scenario where the file becomes empty is when LabVIEW is abruptly terminated (e.g., using the Task Manager). However, I have not been able to recreate this exact null character situation.

This issue has occurred intermittently in a runtime environment, and I am trying to determine if there are other possible causes for this behavior.

Has anyone experienced a similar issue? Are there any best practices for handling file operations in LabVIEW to avoid such situations, especially in cases of unexpected application termination?

 

 This is the corrupt file.

varshinee97_0-1736684046664.png

 

This is the block diagram of the code.

varshinee97_1-1736684421421.png

 

 

0 Kudos
Message 1 of 7
(419 Views)
  • Your flatten to json functions does not look stock. Where is it from?
  • Are there any other code parts that access the file?
  • What is the value of the string when this happens?
  • Can't you just bypass the write if the string does not look right?
  • Maybe you should flush the file after writing to ensure it is committed to disk.
0 Kudos
Message 2 of 7
(375 Views)

The Flatten to JSON string is a function of JKI JSON Toolkit

No other part of the code accesses this particular file. The only scenario I suspect could cause this is when LabVIEW is abruptly terminated (e.g., via Task Manager), but I have not been able to recreate this issue on my system. This makes it challenging to pinpoint the exact cause, as I don't have specific details on the string value at the time of the issue. I am curious if there are other known possibilities or scenarios that might result in such file corruption. 

Thanks for the suggestion, I will add verification of string value and flush the file logic and hope it does not occur again. 

 

0 Kudos
Message 3 of 7
(340 Views)

What are you trying to write? I can imagine e.g. a cluster will be all nulls if it's killed just before it's written (race condition?)

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 4 of 7
(319 Views)

Yes. I am trying to write LabVIEW cluster of different elements.

Even if the cluster (with proper labels) data is uninitialized, I don't think that the Flatten to JSON string will create a string with full of null characters. 

0 Kudos
Message 5 of 7
(309 Views)

No it shouldn't. That's why the thought of some race condition that invalidates it popped up. 🙂

G# - Award winning reference based OOP for LV, for free! - Qestit VIPM GitHub

Qestit Systems
Certified-LabVIEW-Developer
0 Kudos
Message 6 of 7
(303 Views)

Since it's inconsistent, I would do three things:

 

1. Add a manual check to be sure that the string passed out from the JSON conversion does not contain nulls.  Abort with an error if it does, with any extra information you can add from context added to the error.  Maybe pass in extra details from the JSON creation as an extra input for this error generation.

2. Instead of having the "Write to text file" take the path as an input, separate it into an "Open file" node and a "write to text file" node.  

3. Right-click the "write to text file" node and deselect the "convert EOL" option.

 

These might seem silly, but since your whole problem shouldn't be happening in the first place, trying somewhat silly things might be your only remaining options.

0 Kudos
Message 7 of 7
(266 Views)