LabVIEW

cancel
Showing results for 
Search instead for 
Did you mean: 

cluster to a array

Solved!
Go to solution

Hello,

So i have a cluster imported from a JSON file, the cluster is organized like below(left one) which lead to a error when i try to use cluster to array, so my question is is there a easy way to parse that cluster like the right one?

 

seems that inner cluster need to have same variable quantities(but not necessary for same variable name) like the right one?

 

ericyuan_1-1593675072806.png

 

 

0 Kudos
Message 1 of 9
(2,498 Views)

Hi Eric,

 

You can use Variant To Data to convert a cluster to an array of variants.  Each element of the array then contains one field from the cluster.  In this case you would use it twice: firstly to convert the original cluster to an array of "Person" clusters, then within a loop for each of these clusters to extract the Name and Age fields and convert to strings.

 

This method relies on knowing the data type of your fields but since you are showing yours as all strings, this should not be a problem.

 

Cluster to array.png

PsyenceFact

0 Kudos
Message 2 of 9
(2,474 Views)

@ericyuan wrote:

seems that inner cluster need to have same variable quantities(but not necessary for same variable name) like the right one?

 


Elements of an array are all the same, so to convert a cluster to an array, all cluster elements need to be the same.

 

Cluster to Array.PNG

 

This is a solution to what you've asked for.

 

It probably doesn't solve your real problem.

 

In reality, you probably get an arbitrary number of persons, with any number of properties that are or aren't there? You'll probably want to ship the cluster (as it's fixed at compile time), and find a way to convert the JSON to an array (or map) of maps.

 

 

0 Kudos
Message 3 of 9
(2,473 Views)

What do you want the Output to be? An Array of clusters? an Array of Strings?

 

Is every instance of the cluster the same?

---------------------------------------------------

Projektingenieur
Restbust, Simulations and HiL development
Custom Device Developer
0 Kudos
Message 4 of 9
(2,431 Views)

hey PsyenceFact

that seems to be the right solution, thanks for your help, I will have a look check and get back to you soon

0 Kudos
Message 5 of 9
(2,382 Views)

I want to convert them to a array of a string. the instance of the cluster are not quite the same but they do have some same items.

0 Kudos
Message 6 of 9
(2,380 Views)

that's just a simple example for what i really want to do, my real idea is trying to minimize the time that i spend on developing the test rig from project to project by simply write a JSON file and get the test items and then execute the individual test case for each of the items.

Attached is the json file that i want to parse, and luckily by using this parse JSON to cluster. I was able to get a cluster, so my next step is going to parse that cluster into a array, so that I can do test based on the  category of each test case.

 

 

0 Kudos
Message 7 of 9
(2,372 Views)
Solution
Accepted by topic author ericyuan

The standard LabVIEW JSON functionality is not very flexible, especially when your JSON can't be shoehorned into a rigid array of clusters.  Try using JSONtext instead to turn your JSON to an array of subJSON items, then do different things based on what type of item is:

 

JSONtext.png

 

BTW: I see that that your JSON is using strings rather than numbers ("0.05" rather than 0.05).  You should use proper numbers.

0 Kudos
Message 8 of 9
(2,366 Views)

that's AWESOME, thanks for share that information, that should definitely save my workload.

 

 

0 Kudos
Message 9 of 9
(2,321 Views)