G Web Development Software

cancel
Showing results for 
Search instead for 
Did you mean: 

Change the Element names in a Cluster Properties for FlattenToJSON

Solved!
Go to solution

I need to change the element names for the cluster properties for generating a JSON string with FlattenToJSON, but they seem fixed at "Element 1", "Element 2", etc.  Is there a way to change these? I see nothing in the documentation and can't find a way to edit them in G Web.  Here's a screen shot for an example that reads tags and generates a JSON string.  On another note, is there a way to take a tag data type and generate a string for JSON (and reverse the process when a JSON string is read/parsed with UnflattenFromJSON?

 

johnsmith2022_0-1689085387077.png

 

0 Kudos
Message 1 of 6
(2,237 Views)

You can create a cluster constant with the field names you want and then use Cluster Properties Node to write to those field names:

Snippet.png

MilanR_0-1689178034524.png

At the end of the VI I see that you are concatenating the JSON strings together which seems a bit unusual.

 

As an FYI, JSON supports arrays so you can make arrays of clusters instead if that is helpful for your use case:

 

ArrayOfClusters.png

MilanR_1-1689178373702.png

 

 


Milan
0 Kudos
Message 2 of 6
(2,184 Views)

Thank you for your response. The code now looks like the following (I tried attaching a snippet but the drag-and-drop upload never completes):

 

johnsmith2022_0-1689519312979.png

 

 

And produces the following JSON string, which does not pass a JSON validator (there is no root element):

 

[{"tagpath":"Path.Tag1","tagvalue":"1","tagtype":"I32"},{"tagpath":"Path.Tag2","tagvalue":"2.5","tagtype":"Double"},{"tagpath":"Path.Tag3","tagvalue":"20","tagtype":"U64"},{"Tag Value":"Path.Tag4","tagvalue":"String tag","tagtype":"String"},{"Tag Value":"Path.Tag5","tagvalue":"True","tagtype":"Boolean"}]

 

The goal is to produce the following example JSON string derived from the earthquake example which passes a JSON validator:

 

{"jsontype":"TagCollection","metadata":{"scenarioname":"Scenario 1","tagcount":5},"tags":[{"tagpath":"Path.Tag1","tagvalue":"1","tagtype":"I32"},{"tagpath":"Path.Tag2","tagvalue":"2.5","tagtype":"Double"},{"tagpath":"Path.Tag3","tagvalue":"20","tagtype":"U64"},{"Tag Value":"Path.Tag4","tagvalue":"String tag","tagtype":"String"},{"Tag Value":"Path.Tag5","tagvalue":"True","tagtype":"Boolean"}]}

 

It looks like a cluster needs to be built that includes "jsontype" and "metadata" before the for loop that builds the cluster array, but I'm not yet able to get this to work. I also have to go the other way; Unflatten From JSON. There's an example of this in the earthquake G Web App, so I'll start working on that with the example as a guide. Is there another G Web example that uses Unflatten From JSON?

0 Kudos
Message 3 of 6
(2,153 Views)

Some typos in the two JSON files:  The first should be:

 

[{"tagpath":"Path.Tag1","tagvalue":"1","tagtype":"I32"},{"tagpath":"Path.Tag2","tagvalue":"2.5","tagtype":"Double"},{"tagpath":"Path.Tag3","tagvalue":"20","tagtype":"U64"},{"tagvalue":"Path.Tag4","tagvalue":"String tag","tagtype":"String"},{"tagvalue":"Path.Tag5","tagvalue":"True","tagtype":"Boolean"}]

 

And the second:

 

{"jsontype":"TagCollection","metadata":{"scenarioname":"Scenario 1","tagcount":5},"tags":[{"tagpath":"Path.Tag1","tagvalue":"1","tagtype":"I32"},{"tagpath":"Path.Tag2","tagvalue":"2.5","tagtype":"Double"},{"tagpath":"Path.Tag3","tagvalue":"20","tagtype":"U64"},{"tagvalue":"Path.Tag4","tagvalue":"String tag","tagtype":"String"},{"tagvalue":"Path.Tag5","tagvalue":"True","tagtype":"Boolean"}]}

 

I have the load working and it parses the JSON correctly and writes the tags.  I still don't have the save working with the start of the JSON generating correctly before the "tags".  There is some cluster property configuration outside the 'for loop' that I still don't have working.  Any ideas on this?

0 Kudos
Message 4 of 6
(2,138 Views)
Solution
Accepted by topic author johnsmith2022

It's conceptually similar. Build a cluster representation that corresponds to the JSON representation, set the values in the cluster with cluster properties, and call flatten to JSON:

 

MilanR_0-1689611786512.png

 


Milan
0 Kudos
Message 5 of 6
(2,114 Views)

That did the trick.  Thank you very much for your help.

0 Kudos
Message 6 of 6
(2,097 Views)