当前位置: 首页 > 知识库问答 >
问题:

提取复杂JSON

皇甫逸清
2023-03-14

我想把一些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');

我看到了一些例子:例1

但我一直没能让它为我工作。

@斯科尔05

我想你的意思是:但它导致了一个错误: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)

这是我在多个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');

共有1个答案

沃皓轩
2023-03-14

递归可能是最适合这种情况的解决方案。

在传递合适的JSON后,以下函数将为您的sql语句提供键和值的列表。

value = []
key = []
def get_value(json):
    for i,j in json.items():
        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)

然后可以将其直接传递到sql语句中

sqlstatement +=  "INSERT INTO " + TABLE_NAME + " " + "(" + ",".join(key) + ")" + " VALUES " + "(" + ",".join(value) + ")" + ";"

输出:

INSERT INTO Holdingtbl (Nature_of_address,name,natures_of_control,surname,title,middle_name,forename,notified_on,premises,country,locality,postal_code,address_line_1,company_number) VALUES (Agriculture,Testing Testing,ownership-of-shares-50-to-75-percent,Testing2,NEW,Testing,Test,2016-04-06,#,England,TRIAL,### ###,Fake street,01234567);

希望这能回答你的问题!!!

 类似资料:
  • 我得到了一个复杂的JSON字符串,如下所示。 这还不完整。但这怎么读呢?

  • 我有一个.jrxml文件,我想将代码中的一些参数传递给它。我有一个<code>Orde</code>r类,它具有<code>双倍价格</code<、<code>int quantity</code〕和<code>Product Product</code’等字段。情况很简单,当我需要传递价格或数量时,我只需要这样做: 当我尝试传递 时出现问题。我尝试了类似的东西: 还有许多其他的,但我一直得到错误

  • 我有一个复杂的字符串。 ex: AVG( 我要把AVG,MAX和MEAN列在名单上。 也可以有这样的字符串。 平均值(最大值)( 如何以最简单、最优化的方式实现这一点?

  • 问题内容: 我们习惯说运算是O(1)。但是,这取决于哈希实现。默认对象哈希实际上是JVM堆中的内部地址。我们确定声称 O(1)是否足够好? 可用内存是另一个问题。据我从javadocs理解,应该是0.75。如果我们在JVM中没有足够的内存并且超出限制怎么办? 因此,似乎无法保证O(1)。是有意义还是我想念什么? 问题答案: 这取决于很多事情。这通常是 O(1),一个体面的哈希它本身是固定的时间…但

  • 如何最好地为Python编写源代码SDK它应该读取嵌套的XML文件并将内容分成多行。现有的源代码都在行级别上工作,这不是我在XML上下文中需要的。 这是一组XML文件,每个文件都构成一个交易,必须分解为多个记录(订单行、付款等)。

  • 主要内容:时间复杂度,空间复杂度《 算法是什么》一节提到,解决一个问题的算法可能有多种,这种情况下,我们就必须对这些算法进行取舍,从中挑选出一个“最好”的。 算法本身是不分“好坏”的,所谓“最好”的算法,指的是最适合当前场景的算法。挑选算法时,主要考虑以下两方面因素: 执行效率:根据算法所编写的程序,执行时间越短,执行效率就越高; 占用的内存空间:不同算法编写出的程序,运行时占用的内存空间也不相同。如果实际场景中仅能使用少量的内