Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

cara mengonversi beberapa lapisan json bersarang ke tabel sql

json_normalize mengharapkan daftar kamus dan dalam kasus Ebs - ini hanya kamus, jadi kita harus memproses data JSON terlebih dahulu:

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

In [89]: for r in data['Volumes']:
    ...:     if 'Ebs' not in r: # add 'Ebs' dict if it's not in the record...
    ...:         r['Ebs'] = []
    ...:     if not isinstance(r['Ebs'], list): # wrap 'Ebs' in a list if it's not a list 
    ...:         r['Ebs'] = [r['Ebs']]
    ...:

In [90]: data
Out[90]:
{'Volumes': [{'Attachments': [{'AttachTime': '2013-12-18T22:35:00.000Z',
     'DeleteOnTermination': True,
     'Device': '/dev/sda1',
     'InstanceId': 'i-1234567890abcdef0',
     'State': 'attached',
     'Tags': [{'Key': 'Name', 'Value': 'DBJanitor-Private'},
      {'Key': 'Owner', 'Value': 'DBJanitor'},
      {'Key': 'Product', 'Value': 'Database'},
      {'Key': 'Portfolio', 'Value': 'DB Janitor'},
      {'Key': 'Service', 'Value': 'DB Service'}],
     'VolumeId': 'vol-049df61146c4d7901'}],
   'AvailabilityZone': 'us-east-1a',
   'Ebs': [{'AttachTime': '2016-09-14T19:49:11.000Z',
     'DeleteOnTermination': True,
     'Status': 'attached',
     'VolumeId': 'vol-049df61146c4d7901'}],
   'VolumeId': 'vol-049df61146c4d7901',
   'VolumeType': 'standard'}]}

CATATAN:'Ebs': {..} telah diganti dengan 'Ebs': [{..}]

In [91]: e = pd.io.json.json_normalize(data['Volumes'],
    ...:                               ['Ebs'],
    ...:                               ['VolumeId'],
    ...:                               meta_prefix='parent_')
    ...:


In [92]: e
Out[92]:
                 AttachTime DeleteOnTermination    Status               VolumeId        parent_VolumeId
0  2016-09-14T19:49:11.000Z                True  attached  vol-049df61146c4d7901  vol-049df61146c4d7901



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. mysql SUM bidang VARCHAR tanpa menggunakan CAST

  2. Urutan pengurutan default di MySQL (ALTER TABLE ... ORDER BY ...;)

  3. mysql menghitung baris dengan loop

  4. Menu bertingkat dari catatan basis data

  5. Strategi untuk menjaga dua database mySQL (di lokasi terpisah) tetap sinkron?