Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在python中将扁平表转换为嵌套(分层)json?_Python_Json_Nested_Flatten_Hierarchical - Fatal编程技术网

如何在python中将扁平表转换为嵌套(分层)json?

如何在python中将扁平表转换为嵌套(分层)json?,python,json,nested,flatten,hierarchical,Python,Json,Nested,Flatten,Hierarchical,我有一张扁平的桌子。我想将其转换为嵌套的json格式 database schema table_name col_name exploration dbo TestTable name exploration dbo TestTable last_name exploration dbo Table_1 name exploration dbo Table_1

我有一张扁平的桌子。我想将其转换为嵌套的json格式

database     schema table_name          col_name

exploration  dbo    TestTable           name
exploration  dbo    TestTable           last_name
exploration  dbo    Table_1             name
exploration  dbo    Table_1             d_id
exploration  dbo    tblMonitorChange    EventType
exploration  dbo    tblMonitorChange    SchemaName
exploration  dbo    tblMonitorChange    ObjectName
exploration  dbo    tblMonitorChange    ObjectType
exploration  dbo    tblMonitorChange    EventDate
exploration  dbo    tblMonitorChange    SystemUser
exploration  dbo    tblMonitorChange    CurrentUser
exploration  dbo    tblMonitorChange    OriginalUser
ReportServer dbo    Users               UserID
ReportServer dbo    Users               Sid
ReportServer dbo    Users               UserType
ReportServer dbo    Users               AuthType
ReportServer dbo    Users               UserName
ReportServer dbo    Users               ServiceToken
ReportServer dbo    Users               Setting
我正在寻找一个通用的解决方案,而不是硬编码列名


任何帮助都将不胜感激。

我递归地解决了这个问题。此功能经过测试并工作:

def table_to_json(model):
    print('-------------------------------------------------------------')
    doc = {};
    col_names = list(model.columns)
    grouped = model.groupby(col_names[0])[col_names[1]]
    values = grouped.apply(lambda x: set(x.tolist()))
    a = values.shape
    if(len(col_names)==2):
        return dict(values)
    keys = list(grouped.groups.keys())
    for k in keys:
        doc.update({k:table_to_json(model[model[col_names[0]] == k][col_names[1:]])})

    return doc
它将在我的第一篇文章中检查它作为输入,并返回一个字典文档作为输出。这是输出的一部分:

{'exploration': {'dbo': {'Table_1': {'d_id', 'name'},
   'TestTable': {'last_name', 'name'},
   'tblMonitorChange': {'CurrentUser',
    'EventDate',
    'EventType',
    'ObjectName',
    'ObjectType',
    'OriginalUser',
    'SchemaName',
    'SystemUser'}}},
'ReportServer': {'dbo': {'ActiveSubscriptions': {'ActiveID',
    'SubscriptionID',
    'TotalFailures',
    'TotalNotifications',
    'TotalSuccesses'},
    'Batch': {'Action',
    'AddedOn',
    'BatchID',
    'BoolParam',
    'Content',
    'Item',
    'Param',
    'Parent',
    'Properties'}}}}