Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7045377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:31:24+00:00 2026-05-28T02:31:24+00:00

I want to convert a JSON file I created to a SQLite database. My

  • 0

I want to convert a JSON file I created to a SQLite database.

My json file is like this (containing traffic data from some crossroads in my city):

{
"2011-12-17 16:00": {
    "local": "Av. Protásio Alves; esquina Ramiro Barcelos",
    "coord": "-30.036916,-51.208093",
    "sentido": "bairro-centro",
    "veiculos": "automotores",
    "modalidade": "semaforo 50-15",
    "regime": "típico",
    "pistas": "2+c"
    },
"2011-12-19 08:38": {
    "local": "R. Fernandes Vieira; esquina Protásio Alves",
    "coord": "-30.035535,-51.211079",
    "sentido": "único",
    "veiculos": "automotores",
    "modalidade": "semáforo 30-70",
    "regime": "típico",
    "pistas": "3"
    }
}

And I have created nice database with a one-to-many relation with these lines of Python code:

import sqlite3

db = sqlite3.connect("fluxos.sqlite")
c = db.cursor()

c.execute('''create table medicoes
         (timestamp text primary key,
          local text,
          coord text,
          sentido text,
          veiculos text,
          modalidade text,
          pistas text)''')

c.execute('''create table valores
         (id integer primary key,
          quantidade integer,
          tempo integer,
          foreign key (id) references medicoes(timestamp))''')

How can I programmatically read the keys from each "block" in the JSON file (in this case, "local", "coord", "sentido", "veiculos", "modalidade", "regime", "pistas" e "medicoes"), create the database with the columns in that same order, and then insert the rows with the proper values?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:31:24+00:00Added an answer on May 28, 2026 at 2:31 am

    You have this python code:

    c.execute("insert into medicoes values(?,?,?,?,?,?,?)" % keys)
    

    which I think should be

    c.execute("insert into medicoes values (?,?,?,?,?,?,?)", keys)
    

    since the % operator expects the string to its left to contain formatting codes.

    Now all you need to make this work is for keys to be a tuple (or list) containing the values for the new row of the medicoes table in the correct order. Consider the following python code:

    import json
    
    traffic = json.load(open('xxx.json'))
    
    columns = ['local', 'coord', 'sentido', 'veiculos', 'modalidade', 'pistas']
    for timestamp, data in traffic.iteritems():
        keys = (timestamp,) + tuple(data[c] for c in columns)
        print str(keys)
    

    When I run this with your sample data, I get:

    (u'2011-12-19 08:38', u'R. Fernandes Vieira; esquina Prot\xe1sio Alves', u'-30.035535,-51.211079', u'\xfanico', u'automotores', u'sem\xe1foro 30-70', u'3')
    (u'2011-12-17 16:00', u'Av. Prot\xe1sio Alves; esquina Ramiro Barcelos', u'-30.036916,-51.208093', u'bairro-centro', u'automotores', u'semaforo 50-15', u'2+c')
    

    which would seem to be the tuples you require.

    You could add the necessary sqlite code with something like this:

    import json
    import sqlite3
    
    traffic = json.load(open('xxx.json'))
    db = sqlite3.connect("fluxos.sqlite")
    
    query = "insert into medicoes values (?,?,?,?,?,?,?)"
    columns = ['local', 'coord', 'sentido', 'veiculos', 'modalidade', 'pistas']
    for timestamp, data in traffic.iteritems():
        keys = (timestamp,) + tuple(data[c] for c in columns)
        c = db.cursor()
        c.execute(query, keys)
        c.close()
    

    Edit: if you don’t want to hard-code the list of columns, you could do something like this:

    import json
    
    traffic = json.load(open('xxx.json'))
    
    someitem = traffic.itervalues().next()
    columns = list(someitem.keys())
    print columns
    

    When I run this it prints:

    [u'medicoes', u'veiculos', u'coord', u'modalidade', u'sentido', u'local', u'pistas', u'regime']
    

    You could use it with something like this:

    import json
    import sqlite3
    
    db = sqlite3.connect('fluxos.sqlite')
    traffic = json.load(open('xxx.json'))
    
    someitem = traffic.itervalues().next()
    columns = list(someitem.keys())
    columns.remove('medicoes')
    columns.remove('regime')
    
    query = "insert into medicoes (timestamp,{0}) values (?{1})"
    query = query.format(",".join(columns), ",?" * len(columns))
    print query
    
    for timestamp, data in traffic.iteritems():
        keys = (timestamp,) + tuple(data[c] for c in columns)
        c = db.cursor()
        c.execute(query)
        c.close()
    

    The query this code prints when I try it with your sample data is something like this:

    insert into medicoes (timestamp,veiculos,coord,modalidade,sentido,local,pistas) values (?,?,?,?,?,?,?)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have some json data, and i want to convert it into plain text,
I want to convert the following json and put the values into a data
I have a a following data that I want to convert it to JSON
I want to convert the XML file to the JSON format, but I don't
I have an XML file which I want to convert into JSON file using
I'm new to XML parsing and want to convert XML file resulting from a
I want to convert my database query's result array to JSON format in PHP.
I want to convert .json file into .csv file using ruby. Pleases help me
I want to convert a primitive to a string, and I tried: myInt.toString(); This
I want to convert a string into a double and after doing some math

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.