07-11-2023 09:24 AM
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?
Solved! Go to Solution.
07-12-2023 11:13 AM
You can create a cluster constant with the field names you want and then use Cluster Properties Node to write to those field names:
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:
07-16-2023 10:02 AM
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):
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?
07-16-2023 04:17 PM
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?
07-17-2023 11:46 AM
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:
07-17-2023 01:36 PM
That did the trick. Thank you very much for your help.