Я хотел бы проанализировать некоторые JSON в SQL INSERT, однако, из-за того, что разные части данных лежат на разных уровнях, это затрудняет их получение.
Вот файл JSON:
{
"company_number":"01234567",
"data":{
"address":{
"address_line_1":"Fake street",
"country":"England",
"locality":"TRIAL",
"postal_code":"### ###",
"premises":"#"
},
"name":"Testing Testing",
"name_elements":{
"forename":"Test",
"middle_name":"Testing",
"surname":"Testing2",
"title":"NEW"
},
"Nature_of_address":"Agriculture",
"natures_of_control":["ownership-of-shares-50-to-75-percent"],
"notified_on":"2016-04-06"
}
}
Это тот Python, который у меня есть, который работает:
import os
import json
TABLE_NAME = "Holdingtbl"
sqlstatement = ''
test = []
#with open (Basenames,'r') as f:
#with open (json.dumps(x.to_table(), indent=4),'r') as f:
# jsondata = json.loads(f.read())
# print(jsondata)
for line in open('C:\\Users\\[Name]\\Desktop\\Test.json','r',encoding="utf-8"):
test.append(json.loads(line))
#print(test)
for json in test:
keylist = "("
valuelist = "("
firstPair = True
for key, value in json.items():
if not firstPair:
keylist += ", "
valuelist += ", "
firstPair = False
keylist += key
if type(value) in (str, "utf-8"):
valuelist += "'" + value + "'"
else:
valuelist += str(value)
keylist += ")"
valuelist += ")"
sqlstatement += "INSERT INTO " + TABLE_NAME + " " + keylist + " VALUES " + valuelist + "\n"
print(sqlstatement)
И результат:
INSERT INTO Holdingtbl (company_number, data) VALUES ('01234567', {'address': {'address_line_1': 'Fake street, 'country': 'England', 'locality': 'TRIAL', 'postal_code': '### ###', 'premises': ‘#'}, 'name': 'Testing Testing', 'name_elements': {'forename': 'Test', 'middle_name': 'Testing', 'surname': 'Testing2', 'title': 'New'},'Nature_of_address': 'Agriculture' ,'natures of control’: ['ownership-of-shares-50-to-75-percent'], 'notified_on': '2016-04-06'})
Что-то вроде правильно, но мне нужно, чтобы вставка выглядела следующим образом:
INSERT INTO Holdingtbl (Company_number, address_line_1, country, locality, postal_code, premises, name, forename, middle_name, surname, title, nature_of_address, natures_of_control, notified_on) VALUES('01234567', 'Fake street', 'England', 'Trial', '### ###', '#', 'Testing Testing', 'Test', 'Testing', 'Testing2', 'New', 'Agriculture', 'ownership-of-shares-50-to-75-percent', '2016-01-06');
Я видел несколько примеров: Example1
Но яне смог заставить его работать на меня.
@ Skaul05
Я думаю, что вы имеете в виду следующее: но это вызывает ошибку: AttributeError: у объекта 'dict' нет атрибута 'iteritems'
import os
import json
TABLE_NAME = "Holdingtbl"
sqlstatement = ''
test = []
value = []
key = []
def get_value(json):
for i,j in json.iteritems():
if type(j) in (str, "utf-8"):
key.append(i)
value.append(j)
elif type(j) == list:
key.append(i)
value.append(j[0])
elif type(j) == dict:
get_value(j)
#with open (Basenames,'r') as f:
#with open (json.dumps(x.to_table(), indent=4),'r') as f:
# jsondata = json.loads(f.read())
# print(jsondata)
for line in open(''C:\\Users\\[Name]\\Desktop\\Test.json'','r',encoding="utf-8"):
test.append(json.loads(line))
#print(test)
for json in test:
get_value(json)
sqlstatement += "INSERT INTO " + TABLE_NAME + " " + keylist + " VALUES " + valuelist + "\n"
print(sqlstatement)
thisвывод, если я зациклюсь на более чем одной строке json:
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on,company_number,address_line_1,address_line_2,locality,postal_code,region,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30','01605766','Wates House','Station Approach','Leatherhead','KT22 7SW','Surrey','England','16c4017adbe1919073fef3fad7535841299de14c','individual-person-with-significant-control','/company/01605766/persons-with-significant-control/individual/Z6tt_4IDQGaS5MU_wOCCCOj4zyY','Mr Jeremy Wyckham Wright','Jeremy','Wyckham','Wright','Mr','British','significant-influence-or-control','2016-06-29');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on,company_number,address_line_1,address_line_2,locality,postal_code,region,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30','01605766','Wates House','Station Approach','Leatherhead','KT227SW','Surrey','England','16c4017adbe1919073fef3fad7535841299de14c','individual-person-with-significant-control','/company/01605766/persons-with-significant-control/individual/Z6tt_4IDQGaS5MU_wOCCCOj4zyY','Mr Jeremy Wyckham Wright','Jeremy','Wyckham','Wright','Mr','British','significant-influence-or-control','2016-06-29');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on,company_number,address_line_1,address_line_2,locality,postal_code,region,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on,company_number,address_line_1,country,locality,postal_code,premises,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30','01605766','Wates House','Station Approach','Leatherhead','KT22 7SW','Surrey','England','16c4017adbe1919073fef3fad7535841299de14c','individual-person-with-significant-control','/company/01605766/persons-with-significant-control/individual/Z6tt_4IDQGaS5MU_wOCCCOj4zyY','Mr Jeremy Wyckham Wright','Jeremy','Wyckham','Wright','Mr','British','significant-influence-or-control','2016-06-29','10259080','College Avenue','United Kingdom','Oldham','OL8 4DX','54','United Kingdom','231a5a1071ef608f60a79a0fce2c3e21e29f00b9','individual-person-with-significant-control','/company/10259080/persons-with-significant-control/individual/6Fkld0qaXyjm4FhJMsmUFKAJU4I','Dr Muhammad Fayaz Khan','Muhammad','Fayaz','Khan','Dr','British','ownership-of-shares-25-to-50-percent','2016-06-30');
Я ожидал:
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,country,locality,postal_code,premises,region,ceased_on,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('08581893','High Street','Wendover','England','Aylesbury','HP22 6EA','14a','Buckinghamshire','2016-07-01','England','d55168c49f85ab1ef38a12ed76238d68f79f5a01','individual-person-with-significant-control','/company/08581893/persons-with-significant-control/individual/-6HQmkhiomEBXJI2rgHccU67fpM','Mr Quentin Colin Maxwell Solt','Quentin','Colin Maxwell','Solt','Mr','British','ownership-of-shares-25-to-50-percent','2016-06-30');
INSERT INTO Holdingtbl (company_number,address_line_1,address_line_2,locality,postal_code,region,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('01605766','Wates House','Station Approach','Leatherhead','KT22 7SW','Surrey','England','16c4017adbe1919073fef3fad7535841299de14c','individual-person-with-significant-control','/company/01605766/persons-with-significant-control/individual/Z6tt_4IDQGaS5MU_wOCCCOj4zyY','Mr Jeremy Wyckham Wright','Jeremy','Wyckham','Wright','Mr','British','significant-influence-or-control','2016-06-29');
INSERT INTO Holdingtbl (company_number,address_line_1,country,locality,postal_code,premises,country_of_residence,etag,kind,self,name,forename,middle_name,surname,title,nationality,natures_of_control,notified_on) VALUES ('10259080','College Avenue','United Kingdom','Oldham','OL8 4DX','54','United Kingdom','231a5a1071ef608f60a79a0fce2c3e21e29f00b9','individual-person-with-significant-control','/company/10259080/persons-with-significant-control/individual/6Fkld0qaXyjm4FhJMsmUFKAJU4I','Dr Muhammad Fayaz Khan','Muhammad','Fayaz','Khan','Dr','British','ownership-of-shares-25-to-50-percent','2016-06-30');