How to write text with Date to json file without serializing it
up vote
1
down vote
favorite
i am reading a text from .txt
file and after doing some modifications iam want to write it to json file which also has date format in it. when iam trying to writing it to json file using json.dumps
it gives:
Object of type 'datetime' is not JSON serializable
when i am seralising it and writing it to file it works fine but now the date is respresented in string fromat i want to be in json Iso date fromat
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 ?
Edit: 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
python json
|
show 1 more comment
up vote
1
down vote
favorite
i am reading a text from .txt
file and after doing some modifications iam want to write it to json file which also has date format in it. when iam trying to writing it to json file using json.dumps
it gives:
Object of type 'datetime' is not JSON serializable
when i am seralising it and writing it to file it works fine but now the date is respresented in string fromat i want to be in json Iso date fromat
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 ?
Edit: 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
python json
2
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8: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 7 at 9:01
1
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 7 at 9:05
1
See json.org.
– str
Nov 7 at 9: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 7 at 9:30
|
show 1 more comment
up vote
1
down vote
favorite
up vote
1
down vote
favorite
i am reading a text from .txt
file and after doing some modifications iam want to write it to json file which also has date format in it. when iam trying to writing it to json file using json.dumps
it gives:
Object of type 'datetime' is not JSON serializable
when i am seralising it and writing it to file it works fine but now the date is respresented in string fromat i want to be in json Iso date fromat
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 ?
Edit: 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
python json
i am reading a text from .txt
file and after doing some modifications iam want to write it to json file which also has date format in it. when iam trying to writing it to json file using json.dumps
it gives:
Object of type 'datetime' is not JSON serializable
when i am seralising it and writing it to file it works fine but now the date is respresented in string fromat i want to be in json Iso date fromat
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 ?
Edit: 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
python json
python json
edited Nov 7 at 9:02
asked Nov 7 at 8:53
aravind_reddy
1,2961416
1,2961416
2
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8: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 7 at 9:01
1
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 7 at 9:05
1
See json.org.
– str
Nov 7 at 9: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 7 at 9:30
|
show 1 more comment
2
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8: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 7 at 9:01
1
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 7 at 9:05
1
See json.org.
– str
Nov 7 at 9: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 7 at 9:30
2
2
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8:59
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8: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 7 at 9:01
@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 7 at 9:01
1
1
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 7 at 9:05
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 7 at 9:05
1
1
See json.org.
– str
Nov 7 at 9:21
See json.org.
– str
Nov 7 at 9:21
1
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 7 at 9:30
"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 7 at 9:30
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53186125%2fhow-to-write-text-with-date-to-json-file-without-serializing-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Possible duplicate of How to overcome "datetime.datetime not JSON serializable"?
– str
Nov 7 at 8: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 7 at 9:01
1
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 7 at 9:05
1
See json.org.
– str
Nov 7 at 9: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 7 at 9:30