02-24-2016 07:25 PM
Hello all, I have been using the HTTP Client to call an API via HTTP GET in order to receive a JSON string. I want to be able to parse out all the information and data that I get in this JSON string. I believe the JSON string I receive is a JSON Scalar and not an array or object. It looks like a combination of array and object. Here is an example of the data I get back:
{"measurements":[{"voltageRMS":120.12,"currentRMS":11.85,"totalEnergy":121044.295}],"code":"OK","duration":7}
I tried using "unflatten from JSON" however it did not work. I kept on getting multiple errors. Error 1 was that path cannot be found in JSON when I put in currentRMS as an array string in the path even though I am following the help directions. Error 2 is that I have a different data type between JSON and LabView. In one of the LabView examples, there is a JSON array going to multiple unflatten from JSON and it works just fine (as long as it's a JSON array). However, once I use my JSON Scalar, the unflatten from JSON function no longer works.
So I downloaded some addons and used the JSON API from LAVA. I used the JSON API for Scalar but kept on getting errors. One of those errors is that the vi is unable to parse data. I tried this with multiple parsing vi's (i.e. timestamp). I tried using JSON array and got errors as well.
I am completely stuck, any advice would be greatly appreciated. Please help. Thank you.
Solved! Go to Solution.
02-25-2016 01:12 AM
02-25-2016 04:28 AM
Even though you only have 1 item in the array, the 'measurements' field is a JSON array - denoted by square brackets. Remember - the cluster needs to match the structure of the JSON data you are trying to decode exactly (not the format you are hoping for!).
With Unflatten From JSON - you can either convert the entire JSON string, or you can pull out items individually by path (e.g. like an XPath in XML).
Here's a couple of examples:
In the top example - I unflatten the entire cluster and this will give an array of measurements. In the second example, I only pull out the "0th" element from the measurement array as a scalar. Had I not added the '0' into the path array, the data type / output would have been an array of measurements.
02-25-2016 08:30 AM
Thank you so much Sam. Much appreciated.