JSON Toolkit for LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

JSON to LV Cluster with missing JSON elements

I am using "JSON Tree to LAbVIEW Type", and running into a small problem.

The JSON response I receive is dynamic; For example, the response contains a list of comments.  First, there is a "numComments" field.  If numComments > 0, there is a Comments array included in the JSON.  But it numComments=0, the array is not included.

When I run the code, I get an error that the Comments element doesn't exist.

The only thing I can think to do is to modify (or duplicate) the function such that missing elements only provide warnings and not errors.  What do you think?

0 Kudos
Message 1 of 5
(12,646 Views)

Hi Jed_Davidow,

JSON Tree to LabVIEW Type.vi was designed so that decoding and encoding data is done in a one-to-one fashion (i.e. so that the cluster and output of JSON Tree to LabVIEW Type.vi can be used with LabVIEW Type to JSON Tree.vi to reproduce the exact same data).

For any parsing or generation beyond the most trivial it is probably best to use the low level functions instead. The following example can take either type of input and parse the structure correctly based on the value of "numComments":

usinglowlevel.png


Milan
0 Kudos
Message 2 of 5
(9,147 Views)

This is a good solution if I know what is going to be missing.  But even in that case, I will have to include clusters for each possible combination of elements.

I think I will duplicate and modify the function to WARN and not ERR when it encounters a missing element.

0 Kudos
Message 3 of 5
(9,147 Views)

Hi Jed_Davidow,

For best performance and flexibility of parsing the low-level parsing functions are the most robust solution. If there are a lot of exceptions to the parsing then that would be the best way to programmatically check for them. However, this may be snother case that is ideal for having strict and relaxed parsing styles as discussed in the NULL as data in DBL and Strings? post. Having multiple parsing methods may be ideal so that the result is predictable as accumulating a large number of warnings during parsing may be difficult to debug.


Milan
0 Kudos
Message 4 of 5
(9,147 Views)

Hey Everyone,

I've been working on this problem and your code helped me greatly. Here is a fully functional library for converting any cluster to JSON and vice versa which uses your code and some open-g code. Thank you for sharing the code and I'm happy to return the favor. The primary VI to use for converting JSON to the cluster is JsonStringToLabviewCluster.vi.

https://dl.dropbox.com/u/11520212/LabVIEW_JSON.zip

Thanks for your help!

0 Kudos
Message 5 of 5
(9,147 Views)