Search code examples
jsontimestampjqepoch

JSON: convert epoch timestamp - jq: error (at <stdin>): Cannot index string with string "timestamp"


raw file

$ < 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",

I wish the same raw file, but the timestamps in human readable form.

I tried:

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

and got this error for every line in file

(...)
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"

Update after first answer:

Drop the call to 'map'.

'todate' expects its input to be numeric, so you will need to add a call to 'tonumber'.

timestamps still in epoch format, not human readable:

< 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"
  }

Solution

    1. Drop the call to 'map'.

    2. 'todate' expects its input to be numeric, so you will need to add a call to 'tonumber':

      .timestamp |= (tonumber|todate)