07-02-2020 02:33 AM - edited 07-02-2020 02:35 AM
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?
Solved! Go to Solution.
07-02-2020 03:53 AM
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.
PsyenceFact
07-02-2020 03:54 AM
@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.
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.
07-02-2020 08:44 AM
What do you want the Output to be? An Array of clusters? an Array of Strings?
Is every instance of the cluster the same?
07-06-2020 04:57 AM
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
07-06-2020 04:59 AM
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.
07-06-2020 05:10 AM
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.
07-06-2020 06:16 AM - edited 07-06-2020 06:26 AM
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:
BTW: I see that that your JSON is using strings rather than numbers ("0.05" rather than 0.05). You should use proper numbers.
07-07-2020 03:39 AM
that's AWESOME, thanks for share that information, that should definitely save my workload.