Support Questions

Find answers, ask questions, and share your expertise
Announcements
Celebrating as our community reaches 100,000 members! Thank you!

convert timestamp to date format

avatar
"cluster_name" : "ecmlabhw",
        "definition_name" : "datanode_storage",
        "latest_timestamp" : 1498726026988,
        "maintenance_state" : "OFF",
        "original_timestamp" : 1497691710912,
        "service_name" : "HDFS",
        "state" : "CRITICAL",
        "text" : "Remaining Capacity:[3785818112], Total Capacity:[92% Used, 46953267200]"
      }
I want to search original_timestamp and convert into date format in  unix pls help out how to do ?
3 REPLIES 3

avatar
Expert Contributor

@Anurag Mishra

I recommend jq for this. Assuming you have a list of the JSON objects you pasted like this: [{"cluster_name":...}, {"cluster_name": ...}] you can use this command to convert the original_timestamps to date:

jq '.[].original_timestamp |= (. / 1000 | strftime("%Y-%m-%d"))' your.json

To filter by original timestamp you can add this select to the query:

jq '.[].original_timestamp |= (. / 1000 | strftime("%Y-%m-%d")) | map(select(.original_timestamp == "<<YOUR FILTER DATE>>"))' your.json

for example:

jq '.[].original_timestamp |= (. / 1000 | strftime("%Y-%m-%d")) | map(select(.original_timestamp == "2017-06-17"))' your.json

avatar

Hi,

I do not want to use jq instead that I want to do this task using unix can you suggest solution using UNIX ?

avatar
Expert Contributor

Hi,

jq can be found in most Linux distributions. If you want to use basic unix commands, maybe try

date -d $((1497691710912 / 1000))

Or maybe you can use python, it's also part of every distributions.

import json
from datetime import datetime


def timestamp_to_str(timestamp):
    return datetime.fromtimestamp(timestamp / 1000).strftime('%Y-%m-%d')


def search(timestamp):
    with open('a') as f:
        data = json.loads(f.read())
        for cluster in data:
            cluster['original_timestamp'] = timestamp_to_str(cluster['original_timestamp'])
            if cluster['original_timestamp'] == timestamp:
                yield cluster