0

Trying to import data from a JSON File into a column data in a Table using SQLAlchemy.

Version looks kind of like this:

class  JsonTable(declarative_base()):
__tablename__ = "json_table"

id = Column(Integer, primary_key=True)
data = Column(JSON)


json_data = open('U:\\data.json')
data = json.load(json_data)    
for key, value in data.items():
    JsonTable

How to get the single JSON elements into my data column? JSON file looks very basic, like this

{
  "company": "test",
  "number": "123"
}

1 Answer 1

1

You can use mapping interface offered by SQLAlchemy:

metadata = MetaData()

columns = (
    Column('id', Integer, primary_key=True),
    Column('data', JSON, nullable=False),
    ...
)

jsonTable = Table('JsonTable', metadata, *columns)

class JsonTable(object):
    def __init__(self, json_data):    
      json_data = open('U:\\data.json')
      data = json.load(json_data)    
      for key, value in data.iteritems():
        setattr(self, key, value)

mapper(JsonTable, jsonTable)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.