I try to get a List from a config.ini file using JSON in python but when I use " ' " for the string value in my list I get an error. Surprisingly I don't have it when I use " " ".
Python Code :
from configparser import ConfigParser
import json
#set and read the config file
config = ConfigParser()
config.read('config.ini')
#get the list with : ""
thisworks = json.loads(config.get('VALUES','value1'))
#get the list with : ''
thisnotwork = json.loads(config.get('VALUES','value2'))
config.ini file :
[VALUES]
value1 = ["tes1", "test2", "test3"]
value2 = ['tes1', 'test2', 'test3']
The variable "thisnotwork" return this error:
Traceback (most recent call last):
File "U:\Desktop\Nouveau dossier (2)\test.py", line 11, in <module>
thisnotwork = json.loads(config.get('VALUES','value2'))
File "C:\Python37\lib\json\__init__.py", line 348, in loads
return _default_decoder.decode(s)
File "C:\Python37\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Python37\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 2 (char 1)
[Finished in 0.258s]
This can be annoying because json.dumps() return 'string' and not "string". If someone has a solution for this it can be really helpful.
JSON Specification requires double quotes to be used for string values.
I tried json.dumps(['foo', 'bar'])
and it outputs double quotes as expected.