jFiles 2 - help to create matching Clarion structure

Hi

I’m having a problem creating the correct Clarion structure to match the attached JSON.
Tried the jfiles tool to convert from json to clarion, but only a blank result comes up.
Tried it manually but can’t really figure out how to handle labels with different names (labels as id’s)

Any suggestions?

{
  operators: {
    operator1: {
      top: 20,
      left: 20,
      properties: {
        title: 'Operator 1',
        inputs: {},
        outputs: {
          output_1: {
            label: 'Output 1',
          }
        }
      }
    },
    operator2: {
      top: 80,
      left: 300,
      properties: {
        title: 'Operator 2',
        inputs: {
          input_1: {
            label: 'Input 1',
          },
          input_2: {
            label: 'Input 2',
          },
        },
        outputs: {}
      }
    },
  },
  links: {
    link_1: {
      fromOperator: 'operator1',
      fromConnector: 'output_1',
      toOperator: 'operator2',
      toConnector: 'input_2',
    },
  }
}

Hi Neils

The reason you are having issues is because the above is invalid json.

You should have a format like the following

    "operators": {
        "operator1": {
            "top": 20,
            "left": 20,
            "properties": {
                "title": "Operator 1",
                "inputs": {},
                "outputs": {
                    "output_1": {
                        "label": "Output 1"
                    }
                }
            }
        },
        "operator2": {
            "top": 80,
            "left": 300,
            "properties": {
                "title": "Operator 2",
                "inputs": {
                    "input_1": {
                        "label": "Input 1"
                    },
                    "input_2": {
                        "label": "Input 2"
                    }
                },
                "outputs": {}
            }
        }
    },
    "links": {
        "link_1": {
            "fromOperator": "operator1",
            "fromConnector": "output_1",
            "toOperator": "operator2",
            "toConnector": "input_2"
        }
    }
}

This should then play nicely with @Bruce’s jfiles code tool: https://www.capesoft.com/jfilescode

1 Like

What Mark said. Plus, as a tip, you can use https://jsonlint.com/ to validate your JSON if you aren’t sure if it’s valid or not.

Cheers
Bruce

You are definitely both right. But nevertheless, it’s the data structure of the javascript plugin I use - which works really well. But if it’s not json, what is it then?
Please try to see the examples here: http://sebastien.drouyer.com/jquery.flowchart-demo/

Could it be Eclog format??
Never heard of it until now… https://www.eclog.org/
Then I think I have to encode and decode it myself.

if you end up doing that Niels then I would suggest using a recursive approach and make liberal use of StringTheory’s matchBrackets method.

https://www.capesoft.com/docs/StringTheory3/StringTheory.htm#MatchBrackets

I guess an alternative might be to convert it to valid json, but that might end up being just as much work as parsing it yourself.

Thanks.
I am well aware that it is StringTheory that will help me, but I have not seen MatchBrackets until now. Nice - and relatively easy.
The world was different before StringTheory.

indeed that is very true!

So, the “source” in the examples you showed were JavaScript source code. And indeed that’s the way objects are created and set in JavaScript. That’s somewhat different to JSON which is a “string format”. It’s not a huge surprise to note that the JavaScript object notation (ie the way it’s written in the javascript language, and JSON (the string-format-of-javascript-object-notation) resemble each other.

But the use cases are somewhat different. When you are writing actual JavaScript code, as in the “source code” to the examples, you are not using JSON. Fortunately JavaScript has functions to convert from objects, to string, and vice versa. Google for JSON.parse and JSON.stringify.

How this all comes into play depends on the widget itself, and also what you are doing on the “server side”. I presume you are not generating actual JavaScript.js code - so I’m guessing you are generating data that goes into the widget, or data that you receive from the widget. Typically this transfer of data is done using a JSON string, and then handled (using parse or stringify) on the widget side - but it’ll depend a lot on the specific widget you are using.

Perhaps a good question for the webinar?

Thanks Bruce for taking the time to explain.
JSON.stringify and JSON.parse work fine.
But…
It still remains if I can use jFiles to handle the format. I would like to have my “operators” in a queue, but how do I handle different label names in a queue for export via jFIles?

Hi Niels,
I tried to manually deconstruct a complex Json file for my Nest Home Thermostat - brute force did not help me at all…
I finally used Capesoft Jfiles on line formatter and let it do the work - it WORKED. Once again, I had to listen to Bruce “let us do the heavy lifting”.
Jfiles did it all for me - I was able to pull out the data and make the necessary analysis.
Ron Jolda

I very much agree. I have made great use of this amazing tool.
But in this case I don’t really see how to solve it.
My challenge is that I would like to have “operators” as a queue, but since the label ID of each “operator” has different values, the online tool creates it as separate groups and that doesn’t really make sense for what I am trying to achieve.

It’s important to understand that the online tool is just reflecting what the structure is. The structure is passing through each operator with a unique name (operator1, operator2 etc) rather than as an “array” (what we in Clarion would call a Queue.) So the clarion structure matches the JSON - which in your case uses all these groups, not a queue.

Fortunately you have options. Because jFiles is a “tree” style JSON parser, once the JSON is loaded into the object you can iterate through the object using GET and RECORDS to walk along, and down, the tree. Using this approach you can extract one node at a time, and move it into a queue if you like.

In other words you are not bound by matching the Clarion structure to the JSON (that’s just the easiest way), you can take the JSON yourself, and move it into any structure you like.

Thanks Bruce, but there’s still something I don’t quite understand.
How do I get the values via jFiles when I don’t know the names of the Properties, but only know the level?

The technique is known as “walking” the tree. You move through the nodes (using Get), and for each node you can use GetName, GetParentName, GetType, GetValue and so on.
You can "start’ at a specific place (using GetByName)

for an example of this sort of code, look in jfiles.clw in the WalkNames method. This method is particularly interesting because it’s recursive - meaning that it calls itself. As it walks down the nodes, it does a WalkNames of each node it comes across.

We can chat about this in the OpenClarion webinar on Wednesday if you like.

Hi Bruce

Thanks.
That was straight forward to do and now it works - perfect.
Case dismissed.

/Niels