Created on 06-29-2017 12:41 PM - edited 09-16-2022 04:51 AM
"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 ?
Created 07-03-2017 10:57 AM
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.jsonfor example:
jq '.[].original_timestamp |= (. / 1000 | strftime("%Y-%m-%d")) | map(select(.original_timestamp == "2017-06-17"))' your.json
Created 07-03-2017 12:54 PM
Hi,
I do not want to use jq instead that I want to do this task using unix can you suggest solution using UNIX ?
Created 07-04-2017 07:03 AM
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