【问题标题】:Inserting nested json file in postgreSQL在 postgreSQL 中插入嵌套的 json 文件
【发布时间】:2020-08-11 15:48:21
【问题描述】:

我正在尝试在 PostgreSQL 数据库中插入一个嵌套的 json 文件,下面是 json 文件的示例数据。

[
    {
        "location_id": 11111,
        "recipe_id": "LLLL324",
        "serving_size_number": 1,
        "recipe_fraction_description": null,
        "description": "1/2 gallon",
        "recipe_name": "DREXEL ALMOND MILK 32 OZ",
        "marketing_name": "Almond Milk",
        "marketing_description": null,
        "ingredient_statement": "Almond Milk (ALMOND MILK (FILTERED WATER, ALMONDS), CANE SUGAR, CONTAINS 2% OR LESS OF: VITAMIN AND MINERAL BLEND (CALCIUM CARBONATE, VITAMIN E ACETATE, VITAMIN A PALMITATE, VITAMIN D2), SEA SALT, SUNFLOWER LECITHIN, LOCUST BEAN GUM, GELLAN GUM.)",
        "allergen_attributes": {
            "allergen_statement_not_available": null,
            "contains_shellfish": "NO",
            "contains_peanut": "NO",
            "contains_tree_nuts": "YES",
            "contains_milk": "NO",
            "contains_wheat": "NO",
            "contains_soy": "NO",
            "contains_eggs": "NO",
            "contains_fish": "NO",
            "contains_added_msg": "UNKNOWN",
            "contains_hfcs": "UNKNOWN",
            "contains_mustard": "UNKNOWN",
            "contains_celery": "UNKNOWN",
            "contains_sesame": "UNKNOWN",
            "contains_red_yellow_blue_dye": "UNKNOWN",
            "gluten_free_per_fda": "UNKNOWN",
            "non_gmo_claim": "UNKNOWN",
            "contains_gluten": "NO"
        },
        "dietary_attributes": {
            "vegan": "YES",
            "vegetarian": "YES",
            "kosher": "YES",
            "halal": "UNKNOWN"
        },
        "primary_attributes": {
            "protein": 7.543,
            "total_fat": 19.022,
            "carbohydrate": 69.196,
            "calories": 463.227,
            "total_sugars": 61.285,
            "fiber": 5.81,
            "calcium": 3840.228,
            "iron": 3.955,
            "potassium": 270.768,
            "sodium": 1351.208,
            "cholesterol": 0.0,
            "trans_fat": 0.0,
            "saturated_fat": 1.488,
            "monounsaturated_fat": 11.743,
            "polyunsaturated_fat": 4.832,
            "calories_from_fat": 171.195,
            "pct_calories_from_fat": 36.957,
            "pct_calories_from_saturated_fat": 2.892,
            "added_sugars": null,
            "vitamin_d_(mcg)": null
        },
        "secondary_attributes": {
            "ash": null,
            "water": null,
            "magnesium": 120.654,
            "phosphorous": 171.215,
            "zinc": 1.019,
            "copper": 0.183,
            "manganese": null,
            "selenium": 1.325,
            "vitamin_a_(IU)": 5331.357,
            "vitamin_a_(RAE)": null,
            "beta_carotene": null,
            "alpha_carotene": null,
            "vitamin_e_(A-tocopherol)": 49.909,
            "vitamin_d_(IU)": null,
            "vitamin_c": 0.0,
            "thiamin_(B1)": 0.0,
            "riboflavin_(B2)": 0.449,
            "niacin": 0.979,
            "pantothenic_acid": 0.061,
            "vitamin_b6": 0.0,
            "folacin_(folic_acid)": null,
            "vitamin_b12": 0.0,
            "vitamin_k": null,
            "folic_acid": null,
            "folate_food": null,
            "folate_DFE": null,
            "vitamin_a_(RE)": null,
            "pct_calories_from_protein": 6.514,
            "pct_calories_from_carbohydrates": 59.751,
            "biotin": null,
            "niacin_(mg_NE)": null,
            "vitamin_e_(IU)": null
        }
    }
]

当尝试使用以下 postgres 查询复制数据时

\copy table_name 'location of thefile'

出现以下错误

ERROR:  invalid input syntax for type integer: "["
CONTEXT:  COPY table_name, line 1, column location_id: "["

我也尝试了以下方法,但没有运气

INSERT INTO json_table
SELECT [all key fields]
FROM json_populate_record (NULL::json_table,
    '{
      sample data
    }'
);

在 postegreSQL 表中插入这种类型的嵌套 json 文件的最佳简单方法是什么。有没有可以用来插入任何嵌套 json 文件的查询?

【问题讨论】:

    标签: postgresql


    【解决方案1】:

    将 json 插入表中。事实上,我不符合你的期望。

    yimo=# create table if not exists foo(a int,b text);
    CREATE TABLE
    yimo=# insert into foo select * from json_populate_record(null::foo, ('[{"a":1,"b":"3"}]'::jsonb->>0)::json);
    INSERT 0 1
    yimo=# select * from foo;
     a | b 
    ---+---
     1 | 3
    (1 row)
    

    【讨论】:

      猜你喜欢
      • 2022-08-19
      • 1970-01-01
      • 2019-12-02
      • 2018-08-21
      • 2020-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多