Python: How to change all instances of "timestamp" in .JSON file to a date-time object -


i have locationhistory.json file has location data stored. data looks this:

{  "data" : {    "items" : [ {      "kind" : "latitude#location",      "timestampms" : "1374870896803",      "latitude" : 34.9482949,      "longitude" : -85.3245474,      "accuracy" : 2149    }, {      "kind" : "latitude#location",      "timestampms" : "1374870711762",      "latitude" : 34.9857898,      "longitude" : -85.3526902,      "accuracy" : 2016"    }]   } } 

there thousand instances these in file, i'm trying simplify idea. read data in through following code:

json_file = open('locationhistory.json') json_string = json_file.read() json_data = json.loads(json_string)  locations = json_data["data"]["items"] 

now want change occurrences of "timestampms" date-time object. found through answered problems on stackoverflow following code can me this:

datetime.datetime.fromtimestamp(     int("timestampms") ).strftime('%y-%m-%d %h:%m:%s') 

also way:

dateobject = datetime.fromtimestap(timestampms / 1000) otherformat = dateobject.strftime("%y-%m-%dt%h:%m:%sz") 

my problem i'm not familiar json , not know how loop or iterate on occurrences of "timestampms" in locationhistory.json file , change "timestampsms" i.e 1374870896803 i.e 2014-09-03.....

i tried:

for location in locations:    print(datetime.datetime.fromtimestamp(     int("timestampms")    ).strftime("%y-%m-%dt%h:%m:%sz")    ) 

(this gives invalid syntax error when try run it)

thank you

to extract data location dictionary can use method , divide integer 1000 timestamp without milliseconds:

for location in locations:     print(datetime.datetime.fromtimestamp(         int(location.get("timestampms"))/1000     ).strftime("%y-%m-%dt%h:%m:%sz")) 

Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -