LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Unflatten JSON Explainer

Solved!
Go to solution

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!

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

That JSON string looks like a cluster with a cluster inside of it. I was able to parse it this way:

snippet.png

 

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,

Message 2 of 9
(1,129 Views)

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.

0 Kudos
Message 3 of 9
(1,124 Views)
Solution
Accepted by topic author JFWM

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,

Message 4 of 9
(1,108 Views)

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!

0 Kudos
Message 5 of 9
(1,015 Views)

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:

  • populate the entire JSON data into the cluster and then use "Unbundle by Name", or alternatively
  • read a single element using its path ($.sr.stat).

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.

 

bd.png

 

fp.PNG

0 Kudos
Message 6 of 9
(991 Views)

Cool, thanks for that.  I'll try it out.

0 Kudos
Message 7 of 9
(971 Views)

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.

 

0 Kudos
Message 8 of 9
(944 Views)

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

0 Kudos
Message 9 of 9
(923 Views)