Example Code

Parse JSON to Cluster

Code and Documents

Attachment

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

  • LabVIEW 2013 or compatible

 

Steps to Implement or Execute Code

  1. Download and extract "Parse JSON to Cluster v2 - 2013.zip".
  2. To install as a Quick Drop shortcut, move the folder to (Mac) \Users\<username>\Documents\LabVIEW Data\Quick Drop Plugins\ or (Windows) C:\Users\<username>\Documents\LabVIEW Data\Quick Drop Plugins. You may need to create the "Quick Drop Plugins" folder.
  3. To invoke, either select a string control, indicator or constant, or copy JSON data to your clipboard. Then launch Quick Drop [(Mac) Control-Shift-Space - (Windows) Control-Space] and press Control-J.
  4. Alternatively, open the "JSON to Cluster.vi".
  5. Add whole or part of a custom JSON text to the JSON String control and run the VI.
  6. If the process results in an error, try validating your JSON, and try again.

Additional Information or References

front panel 

Screen Shot 2014-02-12 at 4.51.14 PM.pngblock diagram 

4.png

 **This document has been updated to meet the current required format for the NI Code Exchange. **

 

 

 

 

 

 

Fred Visser -- SystemLink R&D -- National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Comments
mike_nrao
Member
Member
on

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|(?<![\\])["])|}|]|{|\[|\".+?\")

danny81299
Member
Member
on

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.

sr_nilabview
Member
Member
on

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.tokenize.png

 

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.

AndyRR
Member
Member
on

It would be great if someone could make a LabVIEW 2010 or LabVIEW 2010 SP1 version of this. Thanks in advance!

Chris_Cilino
Active Participant Active Participant
Active Participant
on

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/

mtitoo
Active Participant
Active Participant
on

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.

 

JSON.png

 

 

mahalakshmi05
Member
Member
on

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.