I have a json that has all values of strings. I have a decoder that takes all the strings and decodes them into the integers, but there is one value that is showing as "01" that I would like to keep as a string. Is it possible to exclude this in the decoder for it to stay as a string and not parse as a integer.
My JSON is below:
{"Data NC": "0", "Date": "10/10/2018", "Open Report": "142", "Date NC1": "40", "New Data": "0", "Some Report": "71", "New Data1": "01", "Date": "10/06/2021 00:00:00 AM"}
I've already tried using: and not isinstance("New Data1", "01) condition, but I got no luck. Is there a way to do this. Code is below:
class Decoder(json.JSONDecoder):
def decode(self, s):
result = super().decode(s) # result = super(Decoder, self).decode(s) for Python 2.x
return self._decode(result)
def _decode(self, o):
if isinstance(o, str):
try:
return int(o)
except ValueError:
return o
elif isinstance(o, dict):
return {k: self._decode(v) for k, v in o.items()}
elif isinstance(o, list):
return [self._decode(v) for v in o]
else:
return o
Using json.loads to load the decoder above:
json_doc = json.loads(doc, cls=Decoder)
Any help would be appreciated.