Vision JSON parameter

Hi,

Is it possible to pass in a JSON structure to a template in Vision. I know this is possible in Perspective, they obviously differ in implementation but hopefully someone could provide a solution.

I know I can pass a dataset to a template which would get me most of the way, but essentially I need to somehow have a nested dataset, which I don’t think is possible.

The JSON structure I am looking to pass is essentially this:

{
  "steps": [
    {
      "stepName": "Step 1",
      "dryTime": 0,
      ...15 other entries
      "robots": [
        {
          "robotName": "Robot 1",
          "robotProcess": 1
        },
        {
          "robotName": "Robot 2",
          "robotProcess": 2
        }
      ],
      "conveyors": [
        {
          "conveyorName": "Conveyor 1",
          "enabled": true
        },
        {
          "conveyorName": "Conveyor 2",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 3",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 4",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 5",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 6",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 7",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 8",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 9",
          "enabled": false
        }
      ]
    },
    {
      "stepName": "Step 2",
      "dryTime": 0,
      ...15 other entries
      "robots": [
        {
          "robotName": "Robot 1",
          "robotProcess": 1
        },
        {
          "robotName": "Robot 2",
          "robotProcess": 2
        }
      ],
      "conveyors": [
        {
          "conveyorName": "Conveyor 1",
          "enabled": true
        },
        {
          "conveyorName": "Conveyor 2",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 3",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 4",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 5",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 6",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 7",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 8",
          "enabled": false
        },
        {
          "conveyorName": "Conveyor 9",
          "enabled": false
        }
      ]
    },
    ...additional steps, up to 15
  ]
}

Maybe it would be possible to pass it as a String an perform some scripting to parse the values I want, but that doesn’t seem the best way?

Any assistance is appreciated.

Edit: Additionally, I need to pass this data back to the parent view for processing. Just to add some extra context.

Edit: Added more truthful data structure.

Do you have a script/query that generates the dataset you care about? I would probably just feed in the bare minimum parameters you need to the template such that the template, using those parameters, can determine the dataset/datapoints you need. This is also a bit more encapsulated than feeding a full dataset from outside the template and now you need to handle making a dataset binding every time you make a new version of the template instead of just providing the params and letting the template make the ds.

From what I see looks like 2 basic params in stepName/dryTime, and then at most just 2 more datasets 1 for robots and 1 for conveyors? Which doesn’t seem that bad.

Hi,

Sorry, I should have explained the data structure some more.

The complete data structure would have approximately 15 entries at the top level, then the array of robots would be 2 as mentioned, but the array of conveyors would have 9 entries.

There could be up to 15 steps in the top level array as well.

Ok my initial point remains insofar as the robot/conveyors seem simple enough to be a dataset and are hopefully derivable via query/script from some params? I mean you must be doing some calculation outside the template to generate the json for this right now. I would recommend feeding the things you need to derive the dataset as your params, and just construct the datasets as internal props of the template.

Regarding sending data back out from the template, my preferred method is message handling now, embracing the pub/sub model helps make a lot of things much cleaner. But if it is something simple, I tend to make a template param like _out_someValue the _out_ prefix so I know that it is an output. Then you can bind other compenents values to that. One sticky issue is inside the template you cannot bind _out_someValue to props, you will need to use property changes to write to it.

Yes, I have a script that generates this JSON structure outside of the template. Ok, I will try your method of feeding in the params separately and then constructing the dataset inside? This might work.

1 Like

It helps you follow the rule of encapsulation which helps make your programming/design cleaner. For templates, functions etc generally speaking you should provide the minimum amount of params needed such that the template can derive the rest itself. It will make your life easier.

There can be exceptions especially in scripting if you have a massive dataset you need to pass around instead of requerying for each time, but with templates it’s basically always the right move imo.

1 Like

Recent versions of 8.1 support Document properties. You might experiment with those as template parameters.

FWIW, JSON is a terrible data format (both bloated and lossy) and is a performance nightmare. It is popular because it is the native transport for browsers, where its bloat and lossy character perfectly matches the environment.

In Vision, you should use native, strictly-typed objects internally, and only encode/decode JSON when communicating outside the Vision client.

5 Likes

Huh, that seems to be exactly what I am looking for but our customer specified 8.1.43 :upside_down_face: I will see if we can push to the latest version, or at the very least 8.1.44 when that was introduced.