Overview
This tool helps to automate this process, by parsing JSON, and scripting the corresponding LabVIEW cluster.
Description
The Unflatten from JSON primitive introduced with LabVIEW 2013 often requires that JSON of interest be mapped to a LabVIEW cluster datatype, before you can unflatten the data in LabVIEW. This tool helps to automate this process, by parsing JSON, and scripting the corresponding LabVIEW cluster.
Requirements
Steps to Implement or Execute Code
Additional Information or References
front panel
block diagram
**This document has been updated to meet the current required format for the NI Code Exchange. **
Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.
I haven't thoroghly tested it, but I modified the regex in the Tokenize method to handle empty strings (pair of double quotes) as a value. This was tripping it up before. Here's the new regex string:
(\".+?\"\s*?:\s*?(\"{2}|\[|{|\".+?\"|[0-9]+|[0-9]+\.[0-9]+|True|False|true|false|(?<![\\])["])|}|]|{|\[|\".+?\")
I haven't thouroughly tested it either, but I was having issues with proper distinction between floatingpoint and decimal types. The issue lies with the part of the string [0-9]+|[0-9]+\.[0-9]+ is mean to match all real numbers. However, it won't match numbers that start with a decimal point, ie 0.7 works, but .7 won't. The modification [0-9]+|[0-9]*\.[0-9]+ is necessary. Furthermore, because regex matches with the shortest expression first, any decimal number matches first with [0-9]+ causing JSON to Token to truncate anything past and including the decimal point. Combining the decimal and floatingpoint parts of the regex string into just [0-9]*\.?[0-9]+ prevents truncation and allows JSON to Token also properly distinguish floatingpoint and decimal. The revised string, including a modification to allow for the values TRUE and FALSE and based off of the string in v2 is as follows:
(\".+?\"\s*?:\s*?(\[|{|\".+?\"|[0-9]*\.?[0-9]+|True|False|true|false|TRUE|FALSE)|}|]|{|\[|\".+?\")
Further modifications are required in order to properly tokenize the input string, the operators need to be modified with additions to the floating operators as
"%s":.%d
"%s": .%d
"%s" : .%d
and more for the boolean operators.
Hi Fred_V,
Your VI is really cool. I have made modifications to include the Date and time stamp into the output cluster, but, I have failed to include the enumerated datatype. Can you please help me out.
Thank you in advance.
I am not able to attach my own code in which I had included the time stamp. Above is the snapshot of the block diagram of the code.
It would be great if someone could make a LabVIEW 2010 or LabVIEW 2010 SP1 version of this. Thanks in advance!
Hey Fred,
This is awesome man! You have no idea how much time this is going to save me as i'm attempting to programmatically generate confluence documentation for my software.
One bug I found. It doesn't look like your regular expression allows for an empty value of an element. So i've edited it to
(\".+?\"\s*?:\s*?(\"\"|\[|{|\".+?\"|\"\"|[0-9]+|[0-9]+\.[0-9]+|True|False|true|false)|}|]|{|\[|\".+?\")
The edit is in blue and bolded
PS for anyone interested in viewing JSON and regular expression I found the following two very helpful sites
Online JSON viewer: http://jsonviewer.stack.hu/
Online regular expression analyzer \ viewer: https://regex101.com/
Failed to create numerical array for this valid JSON {"value":[1,2,3]} The fix was to add [-+][0-9]*\.?[0-9]| at the beginning of the regex. Not fully tested though but works for me.
Here is the full expression.
[-+][0-9]*\.?[0-9]|(\".+?\"\s*?:\s*?(\[|{|\".+?\"|[-+][0-9]*\.?[0-9]+|True|False|true|false|TRUE|FALSE)|}|]|{|\[|\".+?\"). Also need to change the token creation similar to this. I assumed the numbers to be DBL.
[-+] was added to support signed numbers.
Hi, can I have a output terminal with the cluster created so that i could use it as a reference to variant to data conversion function.
My use case is -
I have a json to parse and convert it to labview data type. But the structure of json keeps changing. So each and every time i need to create a cluster and then convert it into labview data type. Is there a way that i can automate this process of cluster creation.
This VI would be helpful for me if i could input the json string and output a cluster in a terminal. Any suggestions or ideas are welcomed please.
Hi, Although it seems to get the structure right the actual values are not populating! do I need something else to get the values?
Hey Mohamedbakr80,
As in the example: Steps to Run: 2. New VI is created that contains cluster representation of the object, and that can be used with the Unflatten from JSON VI.
Just take the generated cluster and connect it with the block: "Unflatten from JSON". It works perfectly.
Already kudo'd this post, but wanted to drop a HUGE thanks for sharing this code. I did have to fiddle with the regex string as indicated in some of the other comments, and also had to manually wrangle the output cluster for a few fields, but this code did 95 % of the work I needed it to do right out-of-the-box with minimal fuss.
Amazing work!