LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing Scalar JSON from HTTP Client

Solved!
Go to solution

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.

0 Kudos
Message 1 of 4
(4,672 Views)
What was the LabVIEW datatype you tried to turn the JSON into?

It looks like you have a cluster containing three items: an array of clusters (which in turn contains three numeric items), a string and a numeric.

Mike...

Certified Professional Instructor
Certified LabVIEW Architect
LabVIEW Champion

"... after all, He's not a tame lion..."

For help with grief and grieving.
0 Kudos
Message 2 of 4
(4,637 Views)
Solution
Accepted by topic author DRH

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:

JSON Unflatten Measurement Data.png

 

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.


LabVIEW Champion, CLA, CLED, CTD
(blog)
Message 3 of 4
(4,623 Views)

Thank you so much Sam.  Much appreciated.

0 Kudos
Message 4 of 4
(4,600 Views)