Home > other >  I want to parse a .js config file in Groovy
I want to parse a .js config file in Groovy

Time:09-17

I'm reading this config from a config.js file and tried to parse. I have tried JsonSlurper with no luck. Can anyone let me know the feasible solution to get this string parsed in groovy

exports.config = {
  tests: './test/lib/test*.js',
  output: './output',
  helpers: {
    WebDriver: {
      url: 'https://google.com/',
      browser: 'chrome',
      host: 'testhost.com',
      port: 80,
    },
  },
  plugins: {
    wdio: {
      enabled: false,
      services: [ 'selenium-standalone' ],
    },
  },
  include: {
    I: './steps_file.js',
  },
  mocha: {},
  name: 'testProject',
  modules: './main/lib/',
  pageobjects: './main/pageObjects/',
  pages: './main/pages/',
  testData: './test/resources/testData/',
};

CodePudding user response:

It looks like it could be viewed as mostly non-conformant json except for the opening assignment and the closing semi-colon (and backticks?)

If these were stripped away, stripping anything before the opening brace and from the end of the string backwards to the closing brace, then you could likely use the LAX parser in jsonslurper which allows for unquoted keys and single quotes.

CodePudding user response:

Stephen and @cfrick, I have used the below to get my requirements achieved :

  1. Used the slurper and used LAX parser to parse the content
  2. Read the config file and removed the "exports.config =".
  3. imported LAX parser and parsed the content new JsonSlurper().setType(RELAX).parseText()
  4. Then used the Map to make it as Hashmap and updated the key using put function Map<String,Object> mappedData=(HashMap<String,Object>)slurpedData; mappedData.put("value" , value);
  • Related