2

I am using Apache commons configuration2 to read and write JSON configuration.

When I am trying to read a JSON file which contains any JSON Array with only one element.

{ 
   "default":{ 
      "recurrence_info":{ 
         "days":[ 
            "SUNDAY"
         ]
      }
   }
}

like in above JSON "default.recurrence_info.days" is an array which contains only one element as "SUNDAY".

I am using below code to get configuration.

 //FileBasedConfigurationBuilder<FileBasedConfiguration>(
         JSONConfiguration.class).configure(new Parameters().hierarchical()
        .setExpressionEngine(engine)
         .setEncoding(CHAR_ENCODING).setThrowExceptionOnMissing(true))
         .setFileName(configFile)).getConfiguration();//

Here configFile is JSON file which contains above JSON.

When I try to get the value of "default.recurrence_info.days" , it is coming as string in place of array.

Finally after doing some debugging , What I understood If any JSON array contains only one element then "Apache commons configuration2" ignore the array and get the element as key(string)value(string) pair.

I want to get it as array . Please let me know What change I can do to get it.

  • I have tried like "configuration.getProperty(default.recurrence_info.days(0)) " and I got "SUNDAY" . But again If I will do "configuration.setProperty(default.recurrence_info.days(0) , "SUNDAY")" , It will convert the JSON array to String key:value pair – saurabh raj Sep 20 '19 at 11:56
  • Did you ever solve this? – Grace Atwood Dec 12 '20 at 02:57

0 Answers0