mardi 3 mai 2016

Writing JSON to SQLite

I'm trying to save a JSON file to a database. I have very little experience but after doing a little searching, I found this code

import json
import sqlite3

JSON_FILE = "some.json"
DB_FILE = "some.db"

traffic = json.load(open(JSON_FILE))
conn = sqlite3.connect(DB_FILE)

foo = traffic[0]["foo"]
bar = traffic[0]["bar"]

data = [foo, bar]

c = conn.cursor()
c.execute('create table table_name (foo, bar)')
c.execute('insert into table_name values (?,?)', data)

conn.commit()
c.close()

However, I'm thrown an error KeyError: 0.

The JSON that I'm testing with is

{
   "name":"",
   "children":[
      {
         "name":"Level 1",
         "children":[
            {
               "name":"Level 2",
               "children":[
                  {
                     "name":"Level 3",
                     "children":[
                        {
                           "name":"Level 4",
                           "children":[
                              {
                                 "name":"Speed",
                                 "children":null,
                                 "id":6
                              }
                           ],
                           "id":5
                        }
                     ],
                     "id":4
                  }
               ],
               "id":3
            }
         ],
         "id":2
      },
      {
         "name":"Level 1",
         "children":[
            {
               "name":"Level 2",
               "children":[
                  {
                     "name":"Level 3",
                     "children":[
                        {
                           "name":"Level 4",
                           "children":[
                              {
                                 "name":"Cost",
                                 "children":null,
                                 "id":11
                              }
                           ],
                           "id":10
                        }
                     ],
                     "id":9
                  }
               ],
               "id":8
            }
         ],
         "id":7
      },
      {
         "name":"Level 1",
         "children":[
            {
               "name":"Level 2",
               "children":[
                  {
                     "name":"Level 3",
                     "children":[
                        {
                           "name":"Level 4",
                           "children":[
                              {
                                 "name":"Manufacturability",
                                 "children":null,
                                 "id":16
                              }
                           ],
                           "id":15
                        }
                     ],
                     "id":14
                  }
               ],
               "id":13
            }
         ],
         "id":12
      }
   ],
   "id":1
}

How exactly would I write this JSON, or a similar one, to a database?

Aucun commentaire:

Enregistrer un commentaire