postgres json解析时间戳

时间:2016-06-06 08:24:23

标签: json postgresql timestamp

我正在尝试从包含json转储的CSV文件中获取一些数据到我的postgres数据库中。只要它只是字符串就可以了,但我希望包含时间戳的字符串存储为postgres中的时间戳。所以我需要对两个字段进行一些转换:registerdate和dateofbirth。以下代码适用于日期转换行...

有关如何将两个字符串成功转换为以下时间戳的任何线索:

CREATE TABLE users (
    id SERIAL,
    mongo_id TEXT,
    password VARCHAR(128),
    firstname VARCHAR(200),
    lastname VARCHAR(200),
    dateofbirth TIMESTAMP,
    registerdate TIMESTAMP,
    displayname VARCHAR(200),
    language VARCHAR(200),
    country VARCHAR(200),
    profilepicture VARCHAR(200),
    backgroundpicture VARCHAR(200),
    type VARCHAR(200),
    sex VARCHAR(6),
    offlinemode BOOLEAN,
    email VARCHAR(200),
    friends VARCHAR(255)[]
);

INSERT INTO users (mongo_id, password,firstname,lastname, dateofbirth, registerdate, displayname, language)
SELECT data->>'_id',
 data->>'password',
  data->>'firstName',
   data->>'secondName',
    to_timestamp(data->'dateOfBirth'->>'$date'),   /*<------*/
     to_timestamp(data->'registerDate'->>'$date'), /*<-------*/
      data->>'displayName',
       data->>'language'
FROM import.mongo_users;

mongo_users中的数据格式:

 { "_id" : "1164", "password" : "aaa123123", "firstName" : "Adam", "secondName" : "Kowlalski", "dateOfBirth" : { "$date" : "2014-05-18T07:41:09.202+0200" }, "registerDate" : { "$date" : "2016-06-01T12:59:53.941+0200" }, "displayName" : "Adam Kowlalski", "language" : "nb", "country" : null, "profilePicture" : null, "backgroundPicture" : null, "type" : "USER", "sex" : "MALE", "offlineMode" : true, "email" : "bk_1164@test.email", "friends" : [ "KUE" ] } 

3 个答案:

答案 0 :(得分:4)

to_timestamp函数需要两个参数:文本格式的date_time和格式化模板。

您不需要使用to_timestamp,因为您的日期时间值已经使用有效的时间戳格式化,并且PostgreSQL能够很好地理解json格式的时间戳。以下效果很好:

SELECT data->>'_id',
 data->>'password',
  data->>'firstName',
   data->>'secondName',
    (data->'dateOfBirth'->>'$date')::timestamp, --<< simply cast to timestamp
     (data->'registerDate'->>'$date')::timestamp, --<< simply cast to timestamp
      data->>'displayName',
       data->>'language'
FROM (SELECT
 '{ "_id" : "1164", "password" : "aaa123123", "firstName" : "Adam", "secondName" : "Kowlalski", "dateOfBirth" : { "$date" : "2014-05-18T07:41:09.202+0200" },
   "registerDate" : { "$date" : "2016-06-01T12:59:53.941+0200" }, "displayName" : "Adam Kowlalski", "language" : "nb", "country" : null, "profilePicture" : null,
   "backgroundPicture" : null, "type" : "USER", "sex" : "MALE", "offlineMode" : true, "email" : "bk_1164@test.email", "friends" : [ "KUE" ] }'::jsonb as data) d

答案 1 :(得分:2)

您的JSON日期格式类似于ISO 8601(https://en.wikipedia.org/wiki/ISO_8601)。要将输入字符串转换为日期变量,您应该使用to_date函数。

e.g。 to_date(data->'dateOfBirth'->>'$date','YYYY-MM-DD"T"HH24:MI:SS')

要知道您是否需要检查时区差异是否起作用。 Postgresql有一个选项OFhttps://www.postgresql.org/docs/current/static/functions-formatting.html

答案 2 :(得分:0)

对我来说这是有效的。

SELECT to_timestamp(nullif(LEFT(dates_json->>'date_prop',10), '')::numeric) as date_extracted FROM table_name

首先将值缩小到10个符号(如果时间戳包括毫秒),然后检查它是否为空,转换为数字,然后将其传递给函数to_timestamp()。 通过这种方式,我修复了另一个错误“日期/时间字段值超出范围”。

相关问题