将多个JSON对象添加到Postgresql中

时间:2016-07-08 19:58:32

标签: json postgresql

我有一个包含数百个Json对象的json文件。

我的postgresql表创建如下:

CREATE TABLE collections(
 id serial,
 collection json);

它可以使用INSERT INTO collections (collection) values (json_object);一次将一个对象添加到表中,但这很乏味且不可持续。什么是更好的方法呢?

我找到的一个解决方案(如explained by this StackOverflow answer)是创建(1)创建临时表和批量json数据(2)创建与键对应的列并添加如下所示的值:

    create temporary table temp_json (values text) on commit drop;
copy temp_json from 'C:\SAMPLE.JSON';

-- remove this comment to insert records into your table
-- insert into tbl_staging_eventlog1 ("EId", "Category", "Mac", "Path", "ID") 

select values->>'EId' as EId,
       values->>'Category' as Category,
       values->>'Mac' as Mac,
       values->>'Path' as Path,
       values->>'ID' as ID      
from   (
           select json_array_elements(replace(values,'\','\\')::json) as values 
           from   temp_json
       ) a;

但是这违背了NoSQL的全部目的。我只是想在每一行上存储一个带有json对象的自动增量id。

1 个答案:

答案 0 :(得分:1)

I figured out a way to do it in Python with the psycopg2 package if anyone is interested. just make sure to fill in the appropriate fields(database, username, password, etc..)

import psycopg2
import json


path_to_file = input('Enter path to json file:')
with open(path_to_file, 'r') as data_file:
    data = json.load(data_file)

collection_array = []
for item in data:
    collection_array.append(json.dumps(item))



try:
    conn = psycopg2.connect(database="", user="", password="",host="127.0.0.1",port="5433")
    print ("opened  database successfully")
    cur = conn.cursor()

    for element in collection_array:
        cur.execute("INSERT INTO my_table (json_column_name) VALUES (%s)", (element,))
    print("successfully inserted records")    


except psycopg2.Error as e:
    raise

finally:
    conn.commit()
    conn.close()
    print("connection is closed")