Error While dumping sql data into json in django - json

I am quite new to django. I am trying to convert sql data fetched from a remote postgresql database into JSON so as to use it in react. But while dumping the data it throws as error.
`AttributeError: 'str' object has no attribute 'get'`
I tried many versions of dumping sql data into json like coneverting data into list and using RealDictCursor but each one of them throws a new error.
Views.py
from django.shortcuts import render, get_object_or_404
from django.http import JsonResponse
from django.http import HttpResponse
from .marketer import marketer
def marketer_list(request):
return JsonResponse(marketer)
marketer.py (function to fetch data and establish the connection)
from django.shortcuts import render, get_object_or_404
import json
import psycopg2
from psycopg2.extras import RealDictCursor
def marketer(self):
connection = psycopg2.connect(user = "db-user",
password = "*****",
host = "18.23.42.2",
port = "5432",
database = "db-name")
cursor = connection.cursor(cursor_factory = RealDictCursor)
postgreSQL_select_Query = "select id from auth_permission"
result = cursor.execute(postgreSQL_select_Query)
#print("Selecting rows from mobile table using cursor.fetchall")
#mobile = dictfetchall(result)
#items = [dict(zip([key[0] for key in cursor.description], row)) for
row in result]
return json.dumps(cursor.fetchall(), indent=2)
Error at Url page
AttributeError: 'str' object has no attribute 'get'
or
in some other methods
is not JSON serializable

Related

TypeError: unhashable type: 'dict' when upload json data

I am trying to add a json file into my Postgres database using sqlalchemy and flask however i'm getting the error. I've created the table Farmers in my pgadmin however now im trying to add the json data in f1.
Error:
line 20, in insert_data
f1 = Farmers(farmers={{"W":1000000,"Z":22758,"J1_I":0.66},{"W":3500000,"Z":21374,"J1_I":2.69},{"W":2500000,"Z":14321,"J1_I":0.76},{"W":2500000,"Z":14321,"J1_I":0.76}})
TypeError: unhashable type: 'dict'
The upload.py file is:
import os
import flask
from flask_sqlalchemy import SQLAlchemy
from flask import Flask, jsonify, send_from_directory
from sqlalchemy.dialects.postgresql import JSON
APP = Flask(__name__)
APP.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://postgres:admin#localhost:5432/flaskwebapp'
db = SQLAlchemy(APP)
class Farmers(db.Model):
id = db.Column(db.Integer, primary_key=True, index=True)
W = db.Column(db.Integer)
Z = db.Column(db.Integer)
J1_I = db.Column(db.Float)
#db.create_all()
def insert_data():
f1 = Farmers(farmers={{"W":1000000,"Z":22758,"J1_I":0.66},{"W":3500000,"Z":21374,"J1_I":2.69},{"W":2500000,"Z":14321,"J1_I":0.76},{"W":2500000,"Z":14321,"J1_I":0.76}})
db.session.add(f1)
db.session.commit()
print('Data inserted to DB!')
insert_data()
Json array object file:
{
"farmers":[ {
"W":1000000,
"Z":22758,
"J1_I":0.66
},
{
"W":3500000,
"Z":21374,
"J1_I":2.69
},
{
"W":2500000,
"Z":14321,
"J1_I":0.76
},
{
"W":2500000,
"Z":14321,
"J1_I":0.76
}]}
Any ideas on how to fix this?
You are trying to store multiple objects in your database at once. For this you have to create an object of type Farmers for each element of the list. You can then store these created objects in the database.
def insert_data():
# The list of objects with their associated data.
farmers = [
{"W":1000000,"Z":22758,"J1_I":0.66},
{"W":3500000,"Z":21374,"J1_I":2.69},
{"W":2500000,"Z":14321,"J1_I":0.76},
{"W":2500000,"Z":14321,"J1_I":0.76}
]
# An object is created for each record and added to a list.
farmer_objects = [Farmers(**data) for data in farmers]
# The objects created are added to the session.
db.session.add_all(farmer_objects)
# The session is closed and the objects it contains are saved.
db.session.commit()
By the way, the JSON file contains an object with a list of objects that contain the data. The list is accessible under the key "farmers". So you have to extract the list from the loaded file first.
For some reason you are trying to pass a dict.

Is there a way how to save json with flask_sqlalchemy with sqlite backend

I am trying to save data in form of JSON (returned as result from POST request)
def get_data(...):
...
try:
_r = requests.post(
_url_list,
headers=_headers
)
return _r.json()
except Exception as ee:
print('Could not get data: {}'.format(ee))
return None
Into a table in SQLITE database as backend.
def add_to_flight_data(_data):
if _data:
try:
new_record = FlightData(data=_data)
db.session.add(new_record)
db.session.commit()
print('Data instertedto DB!')
return "Success"
except Exception as e:
print('Data NOT instertedto DB! {}'.format(e))
pass
This is my simple flask code
import os
import time
import auth
import json
import requests
import datetime
from flask import Flask
from flask_marshmallow import Marshmallow
from flask_sqlalchemy import SQLAlchemy
# from safrs.safrs_types import JSONType
project_dir = os.path.dirname(os.path.abspath(__file__))
database_file = "sqlite:///{}".format(os.path.join(project_dir, "2w.sqlite"))
app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = database_file
db = SQLAlchemy(app)
ma = Marshmallow(app)
class FlightData(db.Model):
id = db.Column(db.Integer, primary_key=True)
created = db.Column(db.DateTime, server_default=db.func.now())
json_data = db.Column(db.JSONType, default={})
def __init__(self, data):
self.data = data
It seems like there is perhaps no option to save JSON in sqlite
json_data = db.Column(db.JSONType, default={})
Please ADVISE
Thanks.
I believe that you should be using db.JSON, not db.JSONType as there is no such column type in sqlalchemy.
Regardless of that, SQLite has no JSON data type, so sqlalchemy won't be able to map columns of type db.JSON onto anything. According to the documentation only Postgres and some MySQL are supported. There is support for JSON in SQLite with the JSON1 extension, but sqlalchemy will not be able to make use of it.
Your best bet then is to declare the column as db.Text and use json.dumps() to jsonify the data on write. Alternatively modify your get_data() function to check for a JSON response (check the Content-type header or try calling _r.json() and catching exceptions), and then return _r.content which will already be a JSON string.
Use json.loads() to read data back from the db.

__str__ returned non-string (type list)

I am having a django app in which I am storing the json variable.I have stored the json variable through admin and I am trying to print it in shell.My main aim is to pass this variable to a webpage with ajax method.But first when I was trying to print it in shell I get this error
__str__ returned non-string (type list)
My models.py is of this form
from django.db import models
from django.utils import timezone
from jsonfield import JSONField
# Create your models here.
class newgrid(models.Model):
data = JSONField()
def __str__(self):
return self.data
My JSON variable is of this form
[{"col":1,"row":1,"size_x":1,"size_y":1},{"col":2,"row":1,"size_x":1,"size_y":1},{"col":3,"row":1,"size_x":1,"size_y":1},{"col":4,"row":1,"size_x":1,"size_y":1},{"col":1,"row":2,"size_x":1,"size_y":1},{"col":2,"row":2,"size_x":1,"size_y":1},{"col":3,"row":2,"size_x":1,"size_y":1},{"col":4,"row":2,"size_x":1,"size_y":1},{"col":1,"row":3,"size_x":1,"size_y":1},{"col":2,"row":3,"size_x":1,"size_y":1},{"col":3,"row":3,"size_x":1,"size_y":1},{"col":4,"row":3,"size_x":1,"size_y":1},{"col":5,"row":1,"size_x":1,"size_y":1}]
In shell I ran following commands
from testforweb.models import newgrid
newgrid.objects.all()
It initially returned this
<QuerySet [<newgrid: newgrid object (1)>]>
But then I added
def __str__(self):
return self.data
Just to see the actual JSON variable.But I am getting the error
How to see the actual JSON variable which I inserted through admin coz I need to send it to webpage as it is
Edit 1
My updated models.py
from django.db import models
from django.utils import timezone
from jsonfield import JSONField
import simplejson as json
# Create your models here.
class newgrid(models.Model):
data = JSONField()
def __str__(self):
json.dumps(self.data)
The __str__ function must return a string:
def __str__(self):
return json.dumps(self.data)
The JSON field will actually decode the JSON into native python types (lists and dictionaries).
The __str___ method is always expected to return a string.
If you want a string representation of the json, you should use json.dumps(self.data) or similar to serialise the data field as the return value of __str__.
Use
def __str__(self):
return '%s' % (self.data)
Instead of
def __str__(self):
return json.dumps(self.data)

How to pull data from MongoDB without the JSON using IPython

I've managed to get this far:
import json
import pymongo
from bson import json_util
from pymongo import Connection
c = Connection()
db = c.test
collection = db.messages
for doc in collection.find({"mailbox":"bass-e"}, { "body" : "true" }):
doc
print doc
But what comes out is a JSON object. What I want is just the data. What packages/methods do I need to use to just get the text in the body column?
import pymongo
from pymongo import Connection
c = Connection()
db = c.test
collection = db.messages
messages = []
for doc in collection.find({"mailbox":"bass-e"}, { "body" : "true" }).limit(3):
messages.append(doc["body"])
for x in messages:
print x

Store JSON input

I'm somewhat new to Django and am trying to get Content Security Policy reporting running on my application and am running into some issues with parsing and storing the JSON violation output. I have all reports POSTing to /csp_reports/ and have built a new app called security_report and added the following to my urls.py:
url(r'^csp_report/$', 'security_report.views.secreport'),
My models look like this:
import os
from django.db import models
from django.contrib.auth.models import User
from django.conf import settings
class security_report(models.Model):
id=models.AutoField(primary_key=True)
received=models.DateTimeField(null=True)
csp_report=models.CharField()
blocked_uri=models.CharField()
column_number=models.CharField()
document_uri=models.CharField()
line_number = models.IntegerField()
original_policy= models.CharField()
referrer = models.CharField()
status_code = models.IntegerField()
violated_directive = models.CharField()
source_file = models.CharField()
script_sample = models.CharField()
class Meta:
app_label = "events"
And my views.py which is not complete and not working is below. I am stuck on how to actually grab each individual parameter and dump it into my database. Is json.loads the right way to do this? If so, how do I perform the save()? I am only testing the blocked_uri in the view for now to test it out. I just keep hitting my except: and getting "not saved"
from security_report.models import security_report
from django.utils import simplejson
from django.http import HttpResponse, HttpRequest
from django.views.decorators.csrf import csrf_exempt, csrf_protect
import json
#csrf_exempt
def secreport(request):
if request.method == "POST":
json_data = simplejson.loads(request.raw_post_data)
try:
data = json_data['csp-report']
blocked_uri = data['blocked-uri']
document_uri = data['document-uri']
referrer = data['referrer']
script_sample = data['script-sample']
violated_directive = data['violated-directive']
source_file = data['source-file']
b = security_report()
b.blocked_uri = blocked_uri
b.save()
response = ("saved to database!")
except:
response = "not saved"
return HttpResponse(response)
else:
return HttpResponse("not a post request")