我有一个json文件,我想从中将数据放入我的postgres表中。 链接到档案http://tempsend.com/0013F2BE73 我的json的结构如下:
{"url": someurl,
"hasMore": false,
"orderItems": [{"orderId": "OD003663828138911500",
"status": "APPROVED",
"subItems": [],
"priceComponents": {"shippingCharge": 30.0,
"customerPrice": 417.0,
"totalPrice": 447.0,
"sellingPrice": 439.0},
"orderItemId": 234207779,
"listingId": "LSTNDNEFJK8E4YZEGPJP2EBHU",
"title": "Masha Women's Nighty",
"price": 447.0,
"dispatchByDate": "2015-08-21T09:13:34+05:30",
"shippingPincode": "700027",
"fsn": "NDNEFJK8E4YZEGPJ",
"sla": 2,
"sku": "NT55-188",
"updatedAt": "2015-08-17T09:13:37+05:30",
"stateDocuments": [""],
"shippingFee": 30.0,
"hold": false,
"orderDate": "2015-08-17T09:12:46+05:30",
"quantity": 1},
{"orderId": ....
我的数据库表结构是:
Create table If Not Exists temp_fk_sale_order(order_id varchar,
status varchar,sub_items varchar,shipping_charge float,customer_price float,
total_price float,selling_price float,order_item_id varchar,
listing_id varchar,title varchar,price float,dispatch_by_date timestamp,
shipping_pincode integer,fsn varchar,sla integer,sku varchar,updated_at timestamp,
state_documents varchar,shipping_fee varchar,hold varchar,order_date timestamp,
quantity integer)
我在ubuntu 14.04上,文件在我的下载中。如何将此文件中的数据插入我的postgres表?
编辑:
我写了一个python代码,它解析json并将所需的值存储在不同的列表中。
我的代码:
import json
import csv
'''Store data in a string and then convert it to json'''
with open('data.json','rb') as data:
data_str = data.read()
data_dict = json.loads(data_str)
'''create a list of orders'''
orders_len = len(data_dict['orderItems'])
order_id = []
status = []
sub_items = []
shipping_charge = []
customer_price = []
total_price = []
selling_price = []
order_item_id = []
listing_id = []
title = []
price = []
dispatch_by_date = []
shipping_pincode = []
fsn = []
sla = []
sku = []
updated_at = []
state_documents = []
shipping_fee = []
hold = []
order_date = []
quantity = []
for num in range(0,orders_len):
order_id.append(data_dict['orderItems'][num]['orderId'])
status.append(data_dict['orderItems'][num]['status'])
sub_items.append(data_dict['orderItems'][num]['subItems'])
shipping_charge.append(data_dict['orderItems'][num]['priceComponents']['shippingCharge'])
customer_price.append(data_dict['orderItems'][num]['priceComponents']['customerPrice'])
total_price.append(data_dict['orderItems'][num]['priceComponents']['totalPrice'])
selling_price.append(data_dict['orderItems'][num]['priceComponents']['sellingPrice'])
order_item_id.append(data_dict['orderItems'][num]['orderItemId'])
listing_id.append(data_dict['orderItems'][num]['listingId'])
title.append(data_dict['orderItems'][num]['title'])
price.append(data_dict['orderItems'][num]['price'])
dispatch_by_date.append(data_dict['orderItems'][num]['dispatchByDate'])
shipping_pincode.append(data_dict['orderItems'][num]['shippingPincode'])
fsn.append(data_dict['orderItems'][num]['fsn'])
sla.append(data_dict['orderItems'][num]['sla'])
sku.append(data_dict['orderItems'][num]['sku'])
updated_at.append(data_dict['orderItems'][num]['updatedAt'])
state_documents.append(data_dict['orderItems'][num]['stateDocuments'])
shipping_fee.append(data_dict['orderItems'][num]['shippingFee'])
hold.append(data_dict['orderItems'][num]['hold'])
order_date.append(data_dict['orderItems'][num]['orderDate'])
quantity.append(data_dict['orderItems'][num]['quantity'])
我可以使用psycopg2连接到我的数据库,但我需要将这些列表作为参数传递。
怎么做?
如果有人可以通过存储过程帮助我做到这一点?这将有助于减少负载和对脚本的依赖。
答案 0 :(得分:2)
PostgreSQL没有直接从文件导入原始json数据的功能。所以你应该编写一个php / python / etc脚本,它从json读取行并将其插入PostgreSQL。