2

I am reading some text from a plain text file. After doing some modifications, I want to write another file containing JSON which also has date format in it.

When I try to convert it to JSON using json.dumps, it gives:

Object of type 'datetime' is not JSON serializable

When I seralise it and write it to file, it works fine. But now the date is represented in string format. I want to be in JSON ISO date format.

Here is my code:

def getDatetimeFromISO(s):
    d = dateutil.parser.parse(s)
    return d

with open('./parsedFiles/Data.json','w+') as f:
    parsedData = []

    for filename in os.listdir('./Data'): 
        parsed = {}
        parsed["Id"] = filename[:-4]
        breakDown = []
        with open('./castPopularityData/'+str(filename),'r') as f1:
            data = ast.literal_eval(f1.read())
            for i in range(0,len(data)):
                data[i]["date"] = getDatetimeFromISO(data[i]['date'])
                data[i]["rank"] = data[i]['rank']
                breakDown.append(data[i])
            parsed["breakDown"] = breakDown    
        parsedData.append(parsed)
        print(parsedData)
    json.dump(parsedData, f, indent=4)

How can i write the ISO date to JSON file?

I don't want to serialize my data, which makes date format into string. I want to write dates as dates itself to JSON file.

bignose
  • 30,281
  • 14
  • 77
  • 110
aravind_reddy
  • 5,236
  • 4
  • 23
  • 39
  • 2
    Possible duplicate of [How to overcome "datetime.datetime not JSON serializable"?](https://stackoverflow.com/questions/11875770/how-to-overcome-datetime-datetime-not-json-serializable) – str Nov 07 '18 at 08:59
  • @str no it doesn't help as it is saying me to serialize before writing to file which when i do i shows date as string format instead of dateFormat – aravind_reddy Nov 07 '18 at 09:01
  • 2
    JSON does not support dates. You can either store your dates as ISO strings or use a different data format than json. – Aran-Fey Nov 07 '18 at 09:05
  • what formats does json support ? – aravind_reddy Nov 07 '18 at 09:11
  • 1
    See http://json.org. – str Nov 07 '18 at 09:21
  • 1
    "what formats does JSON support"...strings, numbers and booleans, pretty much. And objects and arrays to hold them in, obviously. So your date will just become a string when serialised to JSON. – ADyson Nov 07 '18 at 09:30

1 Answers1

3

JSON is ignorant of any date or time type. See the table of Python types and how they map to JSON data types.

To represent any type not native to JSON (such as a date or date+time) in JSON, you must serialise it: convert that value to a sequence of characters with some specific format.

The json.JSONEncoder class allows for extension to meet this need:

To extend this to recognize other objects, subclass and implement a default method with another method that returns a serializable object for o if possible, otherwise it should call the superclass implementation (to raise TypeError).

You have chosen the ISO 8601 serialisation format for representing date values; that is a good choice. The datetime.date type directly supports serialising to ISO representation.

So now you need a JSONEncoder subclass that will recognise datetime.date values, and serialise them as ISO 8601:

import datetime
import json

class MySpecificAppJSONEncoder(json.JSONEncoder):
    """ JSON encoder for this specific application. """

    def default(self, obj):
        result = NotImplemented
        if isinstance(obj, datetime.date):
            result = obj.isoformat()
        else:
            result = json.JSONEncoder.default(self, obj)
        return result

Now your function can use that encoder class:

json.dump(parsed_data, outfile, indent=4, cls=MySpecificAppJSONEncoder)
bignose
  • 30,281
  • 14
  • 77
  • 110