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










share|improve this question




















  • 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

















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










share|improve this question




















  • 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















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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini