Parsing json files, node custom module location

I'd like to parse JSON files, and the only way to do it seems to be about building a custom module.
I couldn't find where the module.js file should be located, and how it is opened, the documentation never mention it.

Thank you.


Check the custom module property in the server panel and indicates the path to your js file module.
Hope it helps

Thank you for your help, unfortunately I don't have a server panel because open-stage-control is launched by node.

It is not a problem

Edit : voici un example Load a json file as an argument (linux script) - #2 by Greenman

That's neat Greenman, I've missed this part of documentation where it says how to get help with this command line:

node open-stage-control --help

Many thanks for your time.

By the way, given the warning:

" -c, --custom-module custom module file to load (.js)
WARNING: custom module can access the file system, use
at your own risk.
I might reconsider JSON parsing from within O-S-C and rather use the node server for doing it...

Hi @patricecolet.

I parse a handful of .json files when my app starts up in order to produce controls. The steps are highly redundant so I'm only going to post one of them. This code appears at the top of my custom_module.js, before the module.exports = { line you see in the example.

app.on('sessionOpened',  (data, client) => {
    palettes = loadJSON('/path/to/src/assets/palettes.json')

  // Palettes tab.
  let switchObject = {};
  palettes.forEach((p, i) => {
    switchObject[] = i
  receive('/EDIT', 'palettes-switch', {
    'values': switchObject,
    'gridTemplate': 6,
    'address': '/palettes',
    'preArgs': `[@{this.value}]`
  }, {clientId:});

The warning is here to discourage using custom modules found on the internet without reading them. If you're writing it yourself, there's no more risk than with any other script you'd write.

@erictheise, that's very kind of you to share your code, I'll use this in projects I'm working on.

1 Like

Thank you @jean-emmanuel for mentionning it, is there a way to load custom-module dynamically or within a session?

There is not, unless you create a custom module that handles this logic by itself.

Hello, I've tried this code, it has been handfull to start somewhere with, meanwhile I'd like to make the following code proposition for people who look at this topic:

app.on('sessionOpened',  (data, client) => {
        const  config = loadJSON('../../../config.json');
        let err = "";
        try {
                compo = config.compositions;
                err = ": JSON is okay";
        catch (e) {
                err = ": JSON is malformed: " + e.message;
        let switchObject = {};
        compo.forEach((p, i) => {
        switchObject[p.title] = i
        receive('/NOTIFY', 'hand',err); 
        receive('/EDIT','compoSwitch', {
        'values' : switchObject
        }, {clientId:});

I've added missing brackets at the end of app.on function and some try/catch to to verify if json is loaded with a notify message, usefull for debbuging.
I've spent some time also to discover that json file path is relative with path where the module resides.