10-27-2021 06:16 AM - edited 10-27-2021 06:25 AM
Hello folks,
I have decided to give it a serious try with the G web dev software.
One of the things I intend to do very often is ingest JSON from webservices in a microservices kind of architecture. I have succeeded in retreiving data, but I have not figured out how to parse arrays of cluster with the "unflatten from json" function. The help webpage does not offer an example of parsing such a type.
To do a proof of method test, I expanded a bit on the JSON from the NI example:
{"0":"abc","1":true,"2":[{"name": "Peter", "age": 12}, {"name": "Angela", "age": 14}, {"name": "Damien", "age": 16}]}
I have tried two methods in line of the provided examples. The one below tries to extract a single element from one of the array elements with the provided path. (not very useful if you ask me)
Yields a parse error:
Another method is to treat the array elements as strings:
This, too, yields an error (-375005), but strangly enough it outputs results as well! It even recognises 3 elements in the array, just not its contents.
I also clicked on "explain error" in the error cluster, but that seems to invoke nothing.
The help page states the following:
If JSON string represents a data type that type/defaults does not accept, such as an array of arrays or an array of mixed types, you cannot use a single Unflatten From JSON node to convert the entire JSON string. However, you can use path to identify and extract items in the JSON string.
Does this imply that I have to sting parse each array element and then do an unflatten from JSON on each element?
Solved! Go to Solution.
10-27-2021 12:12 PM
Hi aartjan,
Unflatten from JSON requires the GWeb Type and the JSON Type to align, so for the given example of:
{"0":"abc","1":true,"2":[{"name": "Peter", "age": 12}, {"name": "Angela", "age": 14}, {"name": "Damien", "age": 16}]}
That aligns with the GWeb type:
And has a diagram as follows:
Additional notes:
You only need to match the values you need in the JSON, extra values are ignored. For example the JSON array data contains "age" but if we don't need it we can leave it out of the GWeb type:
The path is useful if you only want to parse some parts of the data. For example if I only need the array of clusters located at field name "2":
10-27-2021 04:16 PM
Geez! That was the first thing I did and it didn't work! I must have had some small datatype mistake and I got to thinking that the function wouldn't support sub objects! What a waste of time.
But many thanks for the clear answer! The path examples are also very useful.
10-28-2021 07:02 AM
One troubleshooting thing I've taken to doing when forced to use the NI JSON tools (instead of, for example, JSONString by JDP) is to take the cluster constant that I think should work and pass it into a Flatten to JSON node. Then compare that output string with the JSON I'm actually trying to parse and that usually helps me find the difference.