This week I’ve been researching the exchange formats available and those which are compatible with LabVIEW, of course.
In my current applications, we’ve been using in almost all cases XML format, not because they are the “most beautiful, readable, easy to implement format”, but mostly because in LabVIEW, especially with Object-Oriented Programming, working with XML is easy, you don’t need so many extra toolkits and you can avoid lots of dependencies.

These functions accept almost all kinds of data types, but there is a caveat. LabVIEW XML Schema may differ from other applications, so data is not so inter-exchangeable. Not so worrying for me, as it is not my primary usage. But, there are some odd behaviors, such as not converting classes with default data. I tried, really tried. I did write some parser (using RegEx, spoiler alert) to locate the class name and then load configuration inside the XML, do some verification, and other stuff, but the file ends up being so polluted, readable but not readable, yeah, you got the point.
JSON Format. A solution?
Some may say, why not INI files? Why not CSV files? Just tired of them. For small things, they work very well, but I want something more straight forward, not just looking keys or lines for a bigger configuration file. So I am really inclined to the JSON format.
It is noticeable the trend in the usage of the JSON format, in IoT, mobile phones, HTTP protocols and a lot of technologies. In my usage specifically, I would choose for a couple of reasons, mainly:
- Very easy to read;
- It fits elegantly the object-oriented style.
So, I went for it and it is going very well. I am mainly using the LabVIEW built-in functions, as shown below:

This function has an input called “anything”, that does not mean really “anything”. It means in fact anything composed, generally more than one element, a cluster, an array of elements. It does not accept classes, which I would like, and apparently it is not so trivial to do it by hand (there are issues with inheritance, composition and much more).
Available Alternatives
I found only one alternative solution, it is called JSON Object Serialization. There are a couple of downsides: it is paid and it needs a lot of dependencies (not always what you want, there is a good insight about dependencies in Sam Taggart’s blog, check it out). I ran a few tests with this tool and it does work with classes, but when the private data contains refnum types, it throws an error.
Anyways, for my configuration files, I can manage to work with clusters and the primitive functions Flatten to JSON and Unflatten from JSON, but one small thing was bothering me. These functions don’t print out JSON in pretty format. Looking for a solution, I ended in some known community packages, such as I3, JKI JSON, JSONtext. All very good solutions.
I really like using JSONtext from Dr. Powell, but in this particular project, I needed only pretty printing, and not all the functions this library provides, which by the way, gives you many tools to manipulate JSON objects, but it is something extra I would have to add, just for one function. So I went digging on the internet to find an algorithm to do this job, and I found this website, which gave me the direction to perform this simple task.
The development
So, I went for this task and did it like this example on the website above, also using a small search based on RegEx, which by the way you should learn, it is a very fast and powerful tool for dealing with the strings. Two websites I used for validation:
- Regexr – https://regexr.com/
- JSON Formatter & Validator – https://jsonformatter.curiousconcept.com/
In the end, it is just a small VI that can be easily included in the project, without the burden of all the dependencies.
Download the solution
If you want the check out the code. I uploaded it to my gitlab. I wrapped up in a library, and added also the other way around from pretty print, the compact form, removing the line feeds and tabs.
Gitlab – JSON Pretty Printer – https://gitlab.com/felipefoz/json-pretty-printer.lvlib
or
https://www.vipm.io/package/felipefoz_lib_json_pretty_printer/ (Thanks Jim for including)
Leave your comments below and let me know if this code helps you out or if you have any doubts.
Very nice! Good use of Regex. I have just taken to using Dr Powell’s Jsontext. Yes it does introduce a lot of options, but the simple vims seem to work just fine in many cases.
LikeLike
Thanks for commenting Sam. Just like I said, for a simple task was not worth it, but for other jobs, I would use it without any doubt.
LikeLike