1

I have some annoying elements in a JSON file that go something like:

"DateTime" : Date(-62135596800000),
    "ReceivedDateTime" : Date(-62135596800000)

where serialising this using json.Load() results in an error because Date() is unrecognized.

Traceback (most recent call last):
  File "json_parse.py", line 10, in <module>
    data = json.load(data_file)
  File "C:\Python27\lib\json\__init__.py", line 291, in load
    **kw)
  File "C:\Python27\lib\json\__init__.py", line 339, in loads
    return _default_decoder.decode(s)
  File "C:\Python27\lib\json\decoder.py", line 364, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python27\lib\json\decoder.py", line 382, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded

so the easiest thing to do is to remove the Date() wrapper before serialising. I can then convert to proper datetime afterwards.

I can do simple things with str.replace such as:

data.replace("Date(","")

but obviously I am not removing the trailing bracket.

How might I go about doing this?

Cheers.

brucezepplin
  • 9,202
  • 26
  • 76
  • 129
  • That is not a valid generic json. Try just replacing the `Date` itself – Netwave Aug 07 '17 at 10:58
  • Unfortunately I don't have control how the json is created. Annoying but just have to make do – brucezepplin Aug 07 '17 at 11:00
  • @brucezepplin: it is not json. File a bug report if the upstream claims that it is a json. Related: [Convert weird Python date format to readable date](https://stackoverflow.com/q/28482616/4279) – jfs Aug 07 '17 at 11:26

2 Answers2

1

I wrote this code for you, it should solve the problem.

a = '''"DateTime" : Date(-62135596800000),
    "ReceivedDateTime" : Date(-62135596800000)'''


while "Date(" in a: a = (a[:a.index("Date(")+len("Date(")+a[a.index("Date(")+len("Date("):].index(")")] + a[a.index("Date(")+len("Date(")+a[a.index("Date(")+len("Date("):].index(")")+1:]).replace("Date(", "", 1)
Liam
  • 6,009
  • 4
  • 39
  • 53
1

The more readable way would be to use re library and create regex:

import re

text = '''"DateTime" : Date(-62135596800000),
    "ReceivedDateTime" : Date(-62135596800000)'''

pattern = re.compile("Date\((.+)\)")
x = pattern.findall(text)

text2 = text

for i in x:
    text2 = text2.replace("Date("+i+")", i)
artona
  • 1,086
  • 8
  • 13
  • This is broken; it will fail if there is more than one date present on the same line (the regex matches everything from the first `Date(` to the final `)`). Making the regex `"Date\((.+?)\)"` would fix that. And doing a separate search-and-replace step, rather than using `re.sub()` in the first place, is just bizarre and inefficient. – jasonharper Aug 11 '18 at 12:28