I <thought> that I was a fairly experienced LV programmer but am quite stuck trying to unflatten a JSON. For reference I am building a CNC controller for the TinyG board using LabView.
Could someone help me understand how to unflatten this particular JSON?
What if the response from a VISA serial read is not "sr" but is "r" such as:
I've tried wiring up a bunch of scenarios and it crashing saying it cannot find the elements, and I can't quite understand the documentation on using the path.
Thanks for the help!
Solved! Go to Solution.
That JSON string looks like a cluster with a cluster inside of it. I was able to parse it this way:
If the instrument does respond with a different syntax, I expect the unflatten function to fail because it would require a different input cluster. I recommend you do an evaluation before running the string into the Unflatten From JSON function. You can consider using a Regular Expression.
Thanks for the reply! I have been using the regular expression so far as stand alone, though it makes sense to use the regular as a filtering tool then to unflatten the JSON as you've shown as clusters.
How would I use the 'Path' function to just extract one of the elements in those clusters, for instance the "stat" U8?
That is also getting me because the "sr" JSON doesn't always come back with every element every time.
I recommend checking out the JSON example that ships with LabVIEW.
Help > Find Examples.. > Fundamentals > Strings > Unflatten from JSON.vi
You will find there how the path is used to fetch specific data.
All the best,
For closure on this thread, here is the final solution I implemented for the JSON unflatten problem; I used 'match first string' to filter which JSON sequence was incoming, then unflattened in a case structure depending on the result of that. Thanks again for the help!
I realize you marked this as "solved". But, I would recommend using something like the JDP Science JSONtext addon. You can get it with VIPM. With your approach using regex, case structures, etc., IMHO makes it too complicated and harder to scale. Below is how I would implement parsing your VISA text and let the JSON text addon library do all the hard work.
As oscarfonseca mentioned, all you need to do is create a cluster within a cluster for the data. With JSONtext you can create a cluster filled with ALL the different VISA responses. If the JSONtext VI doesn't see any of the cluster elements in the VISA data string, it will return the default data or an empty array for that element (without any errors).
I would recommend making the cluster a TypeDef and set the default values to something that indicates an unexpected/erroneous value (i.e., NaN for DBL). Now you only need to update the TypeDef to easily add functionality for any other JSON data formats.
To parse out a single element, you can:
I would probably parse the entire data once, thus having all the data available in the cluster and using the "Unbundle by Name" to access any value needed.
It looks pretty good but I can't get it working. I tried installing through VIPM and am getting dependancy errors I can't resolve.
Ok I got JSONtext working through VIPM (It also needed the other JDP science common installed)... and it is excellent! Thanks for the notice.
JSONtext by JDP <----- awesome