G Web Development Software

cancel
Showing results for 
Search instead for 
Did you mean: 

json parsing array of cluster

Solved!
Go to solution

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:

aartjan_3-1635333156540.png

 

Another method is to treat the array elements as strings:

aartjan_2-1635332989335.png

 

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?

0 Kudos
Message 1 of 4
(2,446 Views)
Solution
Accepted by aartjan

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:

 

jsonlvtype.PNG

 

And has a diagram as follows:

jsolvtypesnippet.png

 

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:

 

jsonlvtypeignorevalues.png

 

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":

 

jsonlvtypepath.png


Milan
0 Kudos
Message 2 of 4
(2,410 Views)

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.

Message 3 of 4
(2,396 Views)

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.

Message 4 of 4
(2,370 Views)