簡體   English   中英

JSON:轉換時期時間戳-JQ:錯誤(在 <stdin> ):無法索引字符串“ timestamp”的字符串

[英]JSON: convert epoch timestamp - jq: error (at <stdin>): Cannot index string with string “timestamp”

原始文件

$ < galera-healthcheck.stdout.log  jq '.' | head -n 40
{
  "timestamp": "1499779398.544612646",
  "source": "Galera Healthcheck",
  "message": "Galera Healthcheck.Opened DB connection",
  "log_level": 1,
  "data": {
    "dbHost": "0.0.0.0",
    "dbPort": 3306,
    "dbUser": "galera-healthcheck"
  }
}
{
  "timestamp": "1499779398.544713974",
  "source": "Galera Healthcheck",
  "message": "Galera Healthcheck.Serving healthcheck endpoint",
  "log_level": 1,
  "data": {
    "url": "http://0.0.0.0:9200/"
  }
}
{
  "timestamp": "1499779398.544794559",
  "source": "Galera Healthcheck",
  "message": "Galera Healthcheck.Attempting to GET endpoint...",
  "log_level": 1,
  "data": {
    "url": "http://0.0.0.0:9200/"
  }
}
{
  "timestamp": "1499779398.545407295",
  "source": "Galera Healthcheck",
  "message": "Galera Healthcheck.Failed to process request",
  "log_level": 2,
  "data": {
    "error": "Cannot get status from galera"
  }
}
{
  "timestamp": "1499779398.545511246",

我希望使用相同的原始文件,但時間戳以易於閱讀的形式顯示。

我試過了:

< galera-healthcheck.stdout.log  jq 'map(if .timestamp then .timestamp |= todate else . end)'

並為文件中的每一行得到此錯誤

(...)
jq: error (at <stdin>:968): Cannot index string with string "timestamp"
jq: error (at <stdin>:969): Cannot index string with string "timestamp"
jq: error (at <stdin>:970): Cannot index string with string "timestamp"
jq: error (at <stdin>:971): Cannot index string with string "timestamp"
jq: error (at <stdin>:972): Cannot index string with string "timestamp"
jq: error (at <stdin>:973): Cannot index string with string "timestamp"
jq: error (at <stdin>:974): Cannot index string with string "timestamp"
jq: error (at <stdin>:975): Cannot index string with string "timestamp"
jq: error (at <stdin>:976): Cannot index string with string "timestamp"
jq: error (at <stdin>:977): Cannot index string with string "timestamp"
jq: error (at <stdin>:978): Cannot index string with string "timestamp"
jq: error (at <stdin>:979): Cannot index string with string "timestamp"
jq: error (at <stdin>:980): Cannot index string with string "timestamp"
jq: error (at <stdin>:981): Cannot index string with string "timestamp"
jq: error (at <stdin>:982): Cannot index string with string "timestamp"
jq: error (at <stdin>:983): Cannot index string with string "timestamp"
jq: error (at <stdin>:984): Cannot index string with string "timestamp"
jq: error (at <stdin>:985): Cannot index string with string "timestamp"
jq: error (at <stdin>:986): Cannot index string with string "timestamp"
jq: error (at <stdin>:987): Cannot index string with string "timestamp"
jq: error (at <stdin>:988): Cannot index string with string "timestamp"
jq: error (at <stdin>:989): Cannot index string with string "timestamp"
jq: error (at <stdin>:990): Cannot index string with string "timestamp"
jq: error (at <stdin>:991): Cannot index string with string "timestamp"
jq: error (at <stdin>:992): Cannot index string with string "timestamp"
jq: error (at <stdin>:993): Cannot index string with string "timestamp"
jq: error (at <stdin>:994): Cannot index string with string "timestamp"
jq: error (at <stdin>:995): Cannot index string with string "timestamp"
jq: error (at <stdin>:996): Cannot index string with string "timestamp"
jq: error (at <stdin>:997): Cannot index string with string "timestamp"
jq: error (at <stdin>:998): Cannot index string with string "timestamp"

第一個答案后更新:

將呼叫掛斷到“地圖”。

“ todate”期望其輸入為數字,因此您需要添加對“ tonumber”的調用。

時間戳仍為紀元格式,無法人工閱讀:

< galera-healthcheck.stdout.log  jq '(if .timestamp then .timestamp |= tonumber else . end)' | head
{
  "timestamp": 1499779398.5446126,
  "source": "Galera Healthcheck",
  "message": "Galera Healthcheck.Opened DB connection",
  "log_level": 1,
  "data": {
    "dbHost": "0.0.0.0",
    "dbPort": 3306,
    "dbUser": "galera-healthcheck"
  }
  1. 將呼叫掛斷到“地圖”。

  2. “ todate”期望其輸入為數字,因此您需要添加對“ tonumber”的調用:

     .timestamp |= (tonumber|todate) 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM