【问题标题】:The json file can not be inserted into a PostgreSQL table无法将 json 文件插入到 PostgreSQL 表中
【发布时间】:2022-01-27 19:12:57
【问题描述】:
create table tmpdocs1(title text,abstract text,authors text,biblio text,pmid text);

create table tmpdocs2(title text,abstract text,authors text,biblio text,year text,volume text,issue text,fpage text,page text,pmid text);

COPY tmpdocsX  FROM 'location of said json file'

我已经使用了所有我能想到的分隔符,但仍然没有。 这是 json 文件内容之一。 它还有50000个。 最有问题的事情之一是文本中的“'”和“,”。

{"title":"Health inequality in adolescence. Does stratification occur by familial social background, family affluence, or personal social position?","abstract":"Background Two new sets of stratification indicators – family's material affluence and adolescent's personal social position- were compared with traditional indicators of familial social position based on parental occupation and education for their ability to detect health inequality among adolescents. Methods Survey data were collected in the Adolescent Health and Lifestyle Survey in 2003 from nationally representative samples of 12-, 14- and 16-year-old Finns (number of respondents 5394, response rate 71%). Indicators of the familial social position were father's socio-economic status, parents' education, parents' labour market position. Indicators of material affluence were number of cars, vacation travels, and computers in the family, own room and amount of weekly spending money. Adolescent's personal social position was measured as school performance. Measures of health were long-standing illness, overweight, use of mental health services, poor self-rated health and number of weekly health complaints. Ordinal logistic regression analysis was applied to study the associations between stratification indicators and health variables. Results All three groups of indicators of social stratification showed inequality in health, but the strongest associations were observed with the adolescent's personal social position. Health inequality was only partly identifiable by the traditional indicators of familial social position. The direction of the inequality was as expected when using the traditional indicators or personal social position: adolescents from higher social positions were healthier than those from lower positions. The indicators of family's material affluence showed mainly weak or no association with health and some of the indicators were inversely associated, although weakly. Conclusion In addition to traditional indicators describing the socio-structural influences on the distribution of health among adolescents, indicators of family's material affluence should be further developed. Adolescents' personal social position should be included in the studies of health inequalities.","authors":[{"surname":"LK","given_names":"Koivusilta"},{"surname":"AH","given_names":"Rimpelä"},{"surname":"SM","given_names":"Kautiainen"}],"biblio":{"journal":{"title":"BMC Public Health","issn":""},"year":"2006","volume":"6","issue":"","fpage":"110","lpage":"110"},"pmid":"16643660"}, 

【问题讨论】:

  • 你得到的错误信息是什么?
  • 请注意,COPY 适用于专有格式的二进制或文本文件或 CSV 文件。它不适用于 JSON 文件。

标签: postgresql pgadmin-4


【解决方案1】:

COPY 支持“文本”、“CSV”和“二进制”,这些都不是你想要的。

另外,您的 json 无效,它以悬空的逗号结尾。

您可以将文本加载到临时表中,然后将其转换为 JSONB 并解析出来。

create temp table j (j text);
\copy j from json.txt ;
insert into tmpdocs1 select t.* from j, jsonb_populate_record(null::tmpdocs1,substring(j for length(j)-2)::jsonb) t;

substring 只是为了去掉尾随空格和悬空的逗号。大概你会想在实际代码中做一些不同的事情。但是由于您只向我们展示了一条线,所以我没有猜测其余的会是什么样子。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-22
    • 2014-01-24
    • 2015-06-12
    • 2019-11-20
    • 2015-05-09
    • 2021-06-12
    • 2017-09-17
    • 1970-01-01
    相关资源
    最近更新 更多