本文介绍了将Nest Json模型嵌套到SQL表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个示例json.以前,我能够解决附件和标签没有嵌套以及作为独立列的问题.任何帮助深表感谢!

here is an example json. Previously, I was able to solve for Attachments and tags not being nested and as induvidual columns. Any help is deeply appreaciated!

{
    "Volumes": [{
        "AvailabilityZone": "us-east-1a",
        "Attachments": [{
            "AttachTime": "2013-12-18T22:35:00.000Z",
            "InstanceId": "i-1234567890abcdef0",
            "VolumeId": "vol-049df61146c4d7901",
            "State": "attached",
            "DeleteOnTermination": true,
            "Device": "/dev/sda1",

            "Tags": [{
                "Value": "DBJanitor-Private",
                "Key": "Name"
            }, {
                "Value": "DBJanitor",
                "Key": "Owner"
            }, {
                "Value": "Database",
                "Key": "Product"
            }, {
                "Value": "DB Janitor",
                "Key": "Portfolio"
            }, {
                "Value": "DB Service",
                "Key": "Service"
            }]
        }],
            "Ebs": {
                                "Status": "attached",
                                "DeleteOnTermination": true,
                                "VolumeId": "vol-049df61146c4d7901",
                                "AttachTime": "2016-09-14T19:49:11.000Z"
                            },
        "VolumeType": "standard",
        "VolumeId": "vol-049df61146c4d7901"
    }]
}

推荐答案

您可以这样操作:

In [1]: fn = r'D:\temp\.data\40454898.json'

In [2]: with open(fn) as f:
   ...:     data = json.load(f)
   ...:

In [14]: t = pd.io.json.json_normalize(data['Volumes'],
    ...:                               ['Attachments','Tags'],
    ...:                               [['Attachments', 'VolumeId'],
    ...:                                ['Attachments', 'InstanceId']])
    ...:

In [15]: t
Out[15]:
         Key              Value Attachments.InstanceId   Attachments.VolumeId
0       Name  DBJanitor-Private    i-1234567890abcdef0  vol-049df61146c4d7901
1      Owner          DBJanitor    i-1234567890abcdef0  vol-049df61146c4d7901
2    Product           Database    i-1234567890abcdef0  vol-049df61146c4d7901
3  Portfolio         DB Janitor    i-1234567890abcdef0  vol-049df61146c4d7901
4    Service         DB Service    i-1234567890abcdef0  vol-049df61146c4d7901

注意:第二个参数['Attachments','Tags']是指向嵌套记录(data['Values']->Attachments->Tags)的路径,第三个参数[['Attachments', 'VolumeId'], ['Attachments', 'InstanceId']]是指向外部元数据(data['Values']->Attachments->VolumeIddata['Values']->Attachments->InstanceId )

NOTE: second argument ['Attachments','Tags'] is a path to to our nested record (data['Values']->Attachments->Tags) and third argument [['Attachments', 'VolumeId'], ['Attachments', 'InstanceId']] is a path to outer metadata (data['Values']->Attachments->VolumeId, data['Values']->Attachments->InstanceId)

这篇关于将Nest Json模型嵌套到SQL表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:36