0

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/',
};
injecteer
  • 20,038
  • 4
  • 45
  • 89

2 Answers2

0

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.

  • Hi Stephen, Thanks for the answer. I tried the below and was getting the error : import groovy.json.JsonParserType; import groovy.json.JsonSlurper; def obj = { tests: './test/lib/test*.js', output: './output' } def slurper = new JsonSlurper().setType(JsonParserType.LAX) def config = slurper.parse(obj); println(config) Script1.groovy: 5: unexpected token: ./test/lib/test*.js @ line 5, column 21. – Manoj Rajan Sep 06 '21 at 15:50
  • @ManojRajan Please add the code to the question – cfrick Sep 06 '21 at 17:04
0

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);