0

I'm trying to:

  1. import chunk of JSONS
  2. modify their dates from date time objects GMT+2 to unix in utc
  3. save them with the new date

According to this post, I've tried the following:

import os, json, pytz
from datetime import datetime, timezone

path_to_json = 'C:/Users/my_path/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]
for js in json_files:
    with open(os.path.join(path_to_json, js)) as json_file:
        json_text = json.load(json_file)
        json_text["startTime"].replace(tzinfo=timezone.utc)

However, I'm getting the following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-11-191fccafcb88> in <module>()
      8     with open(os.path.join(path_to_json, js)) as json_file:
      9         json_text = json.load(json_file)
---> 10         json_text["startTime"].replace(tzinfo=timezone.utc)

TypeError: replace() takes no keyword arguments

Here's an example for a the JSON's structure:

json_text
{'eventsData':
 [{'LeftHandColor': '00A72BFF',
   'RightHandColor': '00A72BFF',
   'Type': 7,
   'UTCTime': 1533647793167.33},
 
  {'LeftHandColor': None,
   'RightHandColor': None,
   'Type': 4,
   'UTCTime': 1533647944567.94}],
 'startTime': '2018-08-07T13:15:48.652076Z'}
​

Appreciate your help understanding what I'm doing wrong. Thanks

Asaf Lahav
  • 57
  • 2
  • 8
  • 1
    please add a sample of your input. also note that json is a string representation of data. there are no datetime objects in json. you'll have to parse from string to datetime object first. – FObersteiner Apr 16 '21 at 08:09
  • If the date literal includes a timezone you don't need to convert anything. There's no `unix date` either. Date literals are deserialized to the DateTime class used by each language. – Panagiotis Kanavos Apr 16 '21 at 08:13
  • I've edited my post with an example. Hope this shed some light – Asaf Lahav Apr 16 '21 at 08:18
  • 2
    The [standard way of representing dates in JSON](https://tools.ietf.org/html/rfc7493#section-4.3) is the ISO8601 format. Since 2015, that's an actual IETF standard. Why are you trying to use a custom format? Everyone expects ISO8601. That `UTCTime` isn't a time at all, or UTC. It's not even a Unix timestamp. Only your code could decipher its meaning. `startTime` n the other hand is crystal-clear, already UTC – Panagiotis Kanavos Apr 16 '21 at 08:18
  • 2
    you'll want `datetime.fromisoformat(json_text["startTime"].replace('Z', '+00:00'))`, see also https://stackoverflow.com/a/62769371/10197418 – FObersteiner Apr 16 '21 at 08:19
  • Thanks @MrFuppes, but I'm using Python 3.6.4, therefore I'm getting: AttributeError: type object 'datetime.datetime' has no attribute 'fromisoformat' – Asaf Lahav Apr 16 '21 at 08:25
  • 1
    @AsafLahav then you need another way to parse the date literal into a `datetime`. The linked question has several answers. There's no reason to use a custom format that will have to be parsed yet again – Panagiotis Kanavos Apr 16 '21 at 08:26

0 Answers0