简体   繁体   English

为什么在对json数据执行sql插入查询后,postgres表为何在每列中显示Null?

[英]Why postgres table shows Null in every column after executing sql insert query with json data?

I am trying to insert json data in to a postgres table using this query- 我正在尝试使用此查询将json数据插入到postgres表中-

INSERT INTO rf_dsgns 
SELECT * FROM json_populate_recordset(NULL::rf_dsgns,
'[
  {
    "Tracking_ID": 2377125,
    "Constr_Zone": "Cleveland",
    "AF_Name": "PbCleveland_10236716P",       
    "Address": "4755 1/2 Rose Avenue",
    "Zip_Code": 44867,
    "Latitude": 5.8923486,
    "Longitude": -71.71052258,        
  },{
    "Tracking_ID": 2377126,
    "Constr_Zone": "Cleveland",
    "AF_Name": "PggClevelandCLE_25236718P",       
    "Street_Address": "4413 1/3 Clain Avenue",  
    "Zip_Code": 44225,
    "Latitude": 40.88960254,
    "Longitude": -71.20898567,        
  }]');

Data types I have used while creating my table are integer,character,character,character,integer,numeric,numeric respectively. 创建表时使用的数据类型分别为整数,字符,字符,字符,整数,数字,数字。 My create table script is 我的创建表脚本是

CREATE TABLE rf_dsgns
(
    tracking_id integer,
    constr_zone character(300),
    af_name character(300),       
    address character(300),        
    zip_code integer,
    latitude numeric,
    longitude numeric       
);

You most probably create the table without using double quotes for the column names (which is a good thing). 您很可能在不使用双引号的列名的情况下创建表(这是一件好事)。 However, json_populate_recordset() matches case sensitive, and thus the lower case column names in the table are not matched with the mixed case names in the JSON. 但是, json_populate_recordset()区分大小写,因此表中的小写列名称与JSON中的大小写混合名称不匹配。

This: 这个:

create table rf_dsgns ("Tracking_ID" int, "Constr_Zone" text, "AF_Name" text, "Address" text, "Zip_Code" text, "Latitude" numeric, "Longitude" numeric);

SELECT * 
FROM json_populate_recordset(NULL::rf_dsgns,
'[
  {
    "Tracking_ID": 2377125,
    "Constr_Zone": "Cleveland",
    "AF_Name": "PbCleveland_10236716P",       
    "Address": "4755 1/2 Rose Avenue",
    "Zip_Code": 44867,
    "Latitude": 5.8923486,
    "Longitude": -71.71052258
  },{
    "Tracking_ID": 2377126,
    "Constr_Zone": "Cleveland",
    "AF_Name": "PggClevelandCLE_25236718P",       
    "Street_Address": "4413 1/3 Clain Avenue",  
    "Zip_Code": 44225,
    "Latitude": 40.88960254,
    "Longitude": -71.20898567        
  }]');

returns: 返回:

Tracking_ID | Constr_Zone | AF_Name                   | Address              | Zip_Code | Latitude    | Longitude   
------------+-------------+---------------------------+----------------------+----------+-------------+-------------
    2377125 | Cleveland   | PbCleveland_10236716P     | 4755 1/2 Rose Avenue |    44867 |   5.8923486 | -71.71052258
    2377126 | Cleveland   | PggClevelandCLE_25236718P |                      |    44225 | 40.88960254 | -71.20898567

However when the table is created without quotes: 但是,在创建不带引号的表时:

create table rf_dsgns (tracking_id int, constr_zone text, af_name text, address text, zip_code text, latitude numeric, longitude numeric);

Then no columns are matched and everything will be NULL 然后没有列匹配,一切都将为NULL

I would not re-create the table with case sensitive column names. 不会重新创建区分大小写列名的表。 I can see two workarounds: create a new type that uses the quoted identifiers but is identical to the table definition and use that to map the JSON data. 我可以看到两种解决方法:创建一个新的类型,该类型使用带引号的标识符,但与表定义相同,并使用该类型来映射JSON数据。

Use json_array_elements() instead and spell out all column names manually That is a bit more typing but does not duplicate the type definition (and is actually a bit more flexible): 改用json_array_elements()并手动拼写所有列名这会增加一些输入但不会重复类型定义(实际上更灵活):

insert into rf_dsgns
SELECT (j ->> 'Tracking_ID')::int, 
       j ->> 'Constr_Zone',
       j ->> 'AF_Name',
       j ->> 'Addres', 
       j ->> 'Zip_Code',
       (j ->> 'Latitude')::numeric,
       (j ->> 'longitude')::numeric
FROM json_array_elements('.... your json here ') a t(j);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM