S Andrew
S Andrew

Reputation: 7268

How to match all keys related to time in json in Python

I have json data which has key related to time. It looks like below:

{
   "tag": {
         "recordTime": "1433522419",
         "eventtime": "1433522419",
         "activeTime": "1433522419",
         "data": [{
             "dataType": "proximity",
             "dataValue": [{
                   "time":"1433522419",
                   "value": 34
               }, {
                   "time":"1433522419",
                   "value": 39
               }, {
                   "time":"1433522419",
                   "value": 45
               }]
             }]
 }

This data contains time values which are in unix epoch time. I need to convert it into local date time format. Is there any way I can get all these values and convert to date time.

Upvotes: 0

Views: 41

Answers (1)

Deejpake
Deejpake

Reputation: 456

Use datetime

from datetime import datetime   

times = {}

times['recordTime'] = datetime.utcfromtimestamp(jsonObj['tag']['recordTime']).strftime('%Y-%m-%d %H:%M:%S')
times['eventtime'] = datetime.utcfromtimestamp(jsonObj['tag']['eventtime']).strftime('%Y-%m-%d %H:%M:%S')

then you can go through all the tags doing that. You can change the formatting of the date by moving around or removing the %Y-%m-%d part. Full docs here https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior

Upvotes: 1

Related Questions