I am trying to parse what should be JSON into a Python dict. However, the JSON file I am working with is not valid JSON as often there will be missing quotes around the key-value pairs.
HJSON seems to be what I'm looking for, however I am finding it errors if I try to pass any value other than null
or any integer.
Playing around with it using some 'JSON' values I have to work with:
import hjson
# EXAMPLE 1
working = hjson.loads('{ date_closed: null }') # <- THIS WORKS!
print(working)
OrderedDict([('date_closed', None)])
# EXAMPLE 2
works_too = hjson.loads('{ date_closed: 42 }') # <- THIS WORKS!
print(works_too)
OrderedDict([('date_closed', 42)])
# EXAMPLE 3
not_working = hjson.loads('{ date_closed: yes }') # <- ERRORS!
~/hjson/decoder.py in scanKeyName(s, end, encoding, strict)
278
279 if ch == '':
--> 280 raise HjsonDecodeError("Bad key name (eof)", s, end);
281 elif ch == ':':
282 if begin == end:
HjsonDecodeError: Bad key name (eof): line 1 column 21 (char 20)
# EXAMPLE 4
# Using different key name
also_not_working = hjson.loads('{ date_opened: yes }') # <- ERRORS with identical error message as above
# Different value name, showing it's not a 'key' error but a 'value' error
this_works = hjson.loads('{ date_opened: null }') # <- THIS WORKS!
print(this_works)
OrderedDict([('date_opened', None)])
# EXAMPLE 5
doesnt_work = hjson.loads('{ date_opened: None }') # <- ERRORS with identical error message as above
The error message seems incorrect. It is not the
key name
that's problematic (since the same key will sometimes work), but rather thevalue name
.The only values that seem able to be parsed by HJSON are integers (value
42
works) andnull
values.
What am I missing here?