Skip to main content

Using the --json switch with an SFDX command returns JSON to stdout. However, if I consume a command in a node process, e.g. 

 

child_process.execFileSync('sfdx', ['force:limits:api:display', '-u', 'alias', '--json']).toString();

 

It does return the JSON, but due to the CLI formatting if the JSON (i.e. the colours) we get all the ANSI escape codes too (so instead of e.g. { } we get \u001b[97m{ \u001b[97m}) making it more effort to parse. Is there a way to get raw JSON back instead of formatted?

5 comentarios
  1. 5 oct 2020, 7:03

    Hi, I have this method in my node app to parse sfdx commands json response:

    private asyncSfdxCommand(command: string) {

    return new Promise((resolve, reject) => {

    exec(command, (error, standardOutput, standardError) => {

    if (error) {

    reject();

    return;

    } else if (standardError) {

    reject(standardError);

    return;

    }

    resolve(standardOutput);

    });

    });

    }

    And I use it this way:const myResponse = JSON.parse(await this.asyncSfdxCommand(`sfdx force:limits:api:display -u MyExampleUser --verbose --json`));

    In the variable I get myResponse the json perfectly parsed and I can get access to all the properties.

    Hope it helps.

0/9000