06-30-2020 08:57 PM
Hello,
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?
{"sr":{"line":0,"posx":0.006,"posy":0.000,"posz":-7.000,"posa":3.000,"feed":0.000,"vel":62.008,"unit":1,"coor":1,"dist":0,"frmo":0,"momo":0,"stat":5}}
What if the response from a VISA serial read is not "sr" but is "r" such as:
{"r":{"xjm":5000000000.000},"f":[1,0,11,6649]}
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.
06-30-2020 09:17 PM
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.
Regards,
06-30-2020 09:23 PM
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.
06-30-2020 09:38 PM
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,
07-06-2020 08:29 AM
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!
07-06-2020 12:55 PM
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.
07-06-2020 07:27 PM
Cool, thanks for that. I'll try it out.
07-08-2020 05:29 PM
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.
07-09-2020 08:12 AM
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