can someone please explain to me whats happening here and whats the difference between these 2 functions. I am essentially trying to return a django object to my angular controller and so the response has to be in json.
obj = Cuisine.objects.all()
print obj
[<Cuisine: Chinese>, <Cuisine: Indian>, <Cuisine: Japanese>]
With django serializer
from django.core import serializers
obj_json = serializers.serialize('json', obj)
print obj_json
[{"pk": 1, "model": "swings.cuisine", "fields": {"name": "Chinese"}}, {"pk": 2, "model": "swings.cuisine", "fields": {"name": "Indian"}}, {"pk": 3, "model": "swings.cuisine", "fields": {"name": "Japanese"}}]
With python json, why is it unable to convert my object to json here?
import json
obj_json = json.dumps(obj)
TypeError: TypeErro...izable',)
Now django-angular has a JSONResponseMixin which automatically converts the returned object / list to json but it keeps giving me a type error. I have a feeling its connected to the same reason why json.dumps does work. I've been reading around plenty trying all sorts of work arounds to no luck. I have already tried converting my object to a list first via list(obj) but that does nothing.
Any ideas?
Related
I'm trying to get the value of ["pooled_metrics"]["vmaf"]["harmonic_mean"] from a JSON file I want to parse using python. This is the current state of my code:
for crf in crf_ranges:
vmaf_output_crf_list_log = job['config']['output_dir'] + '/' + build_name(stream) + f'/vmaf_{crf}.json'
# read the vmaf_output_crf_list_log file and get value from ["pooled_metrics"]["vmaf"]["harmonic_mean"]
with open(vmaf_output_crf_list_log, 'r') as json_vmaf_file:
# load the json_string["pooled_metrics"] into a python dictionary
vm = json.loads(json_vmaf_file.read())
vmaf_values.append((crf, vm["pooled_metrics"]["vmaf"]["harmonic_mean"]))
This will give me back the following error:
AttributeError: 'dict' object has no attribute 'loads'
I always get back the same AttributeError not matter if I use "load" or "loads".
I validated the contents of the JSON, which is valid using various online validators, but still, I am not able to load the JSON for further parsing operations.
I expect that I can load a file that contains valid JSON data. The content of the file looks like this:
{
"frames": [
{
"frameNum": 0,
"metrics": {
"integer_vif_scale2": 0.997330,
}
},
],
"pooled_metrics": {
"vmaf": {
"min": 89.617207,
"harmonic_mean": 99.868023
}
},
"aggregate_metrics": {
}
}
Can somebody provide me some advice onto this behavior, what does it seem so absolutely impossible to load this JSON file?
loads is a method for the json library as the docs say https://docs.python.org/3/library/json.html#json.loads. In this case you are having a AttributeError this means that probably you have created another variable named "json" and when you call json.loads is calling that variable hence it won't have a loads method.
I'm trying to write an efficient stringification routine for logging dicts, but want to redact certain values based on key names. I see that JSONDecoder provides the object_pairs_hook which provides key and value, but I don't see a corresponding hook for JSONEncoder - just 'default' which only provides value. In my case, the values are just other strings so can't base the processing on that alone. Is there something I missed?
For example, if I have a dict with:
{
"username": "Todd",
"role": "Editor",
"privateKey": "1234ad1234e434134"
}
I would want to log:
'{"username":"Todd","role":"Editor","privateKey":"**redacted**"}'
Any good tools in python to do this? Or should I just recursively iterate the (possibly nested) dict directly?
You can "reload" it using the object hook then dump it again.
def redact(o):
if 'privateKey' in o:
o['privateKey'] = '***redacted***'
return o
>>> d
{'username': 'Todd', 'role': 'Editor', 'privateKey': '1234ad1234e434134', 'foo': ['bar', {'privateKey': 'baz'}]}
>>> json.dumps(json.loads(json.dumps(d), object_hook=redact))
'{"username": "Todd", "role": "Editor", "privateKey": "***redacted***", "foo": ["bar", {"privateKey": "***redacted***"}]}'
JSON library has two functions viz. dumps() and loads(). To convert a json to a string use dumps() and vice-versa use loads().
import json
your_dict = {
"username": "Todd",
"role": "Editor",
"privateKey": "1234ad1234e434134"
}
string_of_your_json = json.dumps(your_dict)
I'm New on kivy. I want to save my settings of KivyApplication in a JSON file.
I want to create a JSON file like this:
{
"users": [
{
"user_name": "person_1",
"password": "1234"
},
{
"user_name": "person_2",
"password": "5678"
}
]
}
I found an example in Kivy API references web page (Kivy JSON Storage Example).
Here is my solution for add multiple JSON Objects to the Main JSON Object:
JsonStore.py:
from kivy.storage.jsonstore import JsonStore
store = JsonStore('hello.json')
users_list = [{"user_name": "person_1", "password": "1234"},
{"user_name": "person_2", "password": "5678"}]
# put some values
for u in users_list:
print(u)
store.put('users', u)
But this error occur:
store.put('users', u)
TypeError: put() takes 2 positional arguments but 3 were given
Does anyone know what am I doing wrong here and how to make this work? Thank you in advance...
The structure is kind of predefined by put(object_name, attribute1=value1[, attribute2=value2, ...]).
To accomplish what you want, you have to give users a key to hold your list of objects.
Long story short, this code should work:
from kivy.storage.jsonstore import JsonStore
store = JsonStore('hello.json')
users_list = [{"user_name": "person_1", "password": "1234"},
{"user_name": "person_2", "password": "5678"}]
store.put('users', objects=users_list)
I have a python script that prints JSON and string:
# script.py
print('{"name": "bob", "height": 4, "weight": 145}')
print('hello')
sys.stdout.flush()
A nodejs app calls the python script via child-process. But I'm getting error on the output. How can I process the python output in nodejs?
// nodejs
var process = spawn('python3', ["./script.py", toSend]);
process.stdout.on('data', function(data) {
message = JSON.parse(data)
console.log(message)
})
I'm getting this a SyntaxError: Unexpected token from running this.
In your python script...
This line
print('{"name": "bob", "height": 4, "weight": 145}')
should be changed
import json
print(json.dumps({"name": "bob", "height": 4, "weight": 145}))
That will handle and make sure the JSON is formatted correctly, so that the JSON string can be parsed by node (but your current version should be fine). However in this case the real problem is what follows...
You are ending your script with
print('hello')
which means that JSON.parse() is going to try and parse hello as part of the JSON.parse() because you are reading from stdout... hello is not JSON formatted. So JSON.parse() is going to fail. So remove that line as well.
If you have more then one json object to send as you stated in your comments
You can either combine all the data into a single JSON object
my_object = {"data": "info"....} and json.dumps() that single larger object..
or
obj1 = {}
obj2 = {}
myobjects = [obj1, obj2]
print(json.dumps(myobjects))
and the Node side will recieve a list of objects that can be iterated on
Hi I have just started experimenting with python and tornado along with mongodb(I am a newbie). I have written a simple get function to get all the values from my mongodb and return it in JSON format. The problem is when I try to write the output as a JSON string I get a trailing comma(,) after the last record from the collection.
class TypeList(APIHandler):
#gen.coroutine
def get(self):
cursor = db.vtype.find()
self.write("{"'"success"'": 1, "'"data"'":[")
while (yield cursor.fetch_next):
document = cursor.next_object()
self.write(format(JSONEncoder().encode(document)))
self.write(",")
self.write("]}")
class JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o,ObjectId):
return str(o)
return json.JSONEncoder.default(self, o)
And my output is like
{"success": 1, "data":[{"_id": "55a5e988545779f35d3ecdf4", "name": "fgkd", "city": "fghj"},{"_id": 12345.0, "name": "adfs", "city": "asd"},]}
Can anyone tell me how can I get rid of that trailing comma(,) after my last record, because of that comma I am getting an error malformed JSON string
I have tried using json dumps
#gen.coroutine
def get(self):
cursor = db.vtype.find({"brand": "Tata"})
while (yield cursor.fetch_next):
document = cursor.next_object()
self.write(json.dumps(document,default=json_util.default))
got the output as
{"Reg": "11ts", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}, "Name": "Alex"}{"Reg": "12ts", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}, "Name": "asdf"}
When using dumps[{ "data": document }]
I am getting the output as
[{"data": {"Name": "asdf", "Reg": "asdfs", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}}}]
[{"data": {"Name": "qwer", "Reg": "asdff", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}}}]
but I want the output like this
{"data": [{"Name": "asdf", "Reg": "asdfs", "_id": {"$oid": "55a5e988545779f35d3ecdf4"}},{"Name": "qwer", "Reg": "asdff", "_id": {"$oid": "55a5eac6545779f35d3ecdf5"}}]}
If I am doing something wrong please tell me I dont know how to do it.
There is no reason you should be building up JSON documents via text concatenation.
Python has a perfectly good json module in the standard library which you should be using. Build up your document as a Python list of dicts, then use json.dumps() to convert the whole thing to valid JSON.
So your problem is with MongoDB ObjectId? Then maybe you should have been using bson.json_util. It's probably already installed as part of your MongoDB driver dependecies ( which all use pymongo ), but if not then install it.
import bson
import bson.json_util
from bson.json_util import dumps
from bson import ObjectId
dumps({ "a": ObjectId() })
'{"a": {"$oid": "55a782261d41c80b0432b811"}}'
Or:
dumps([{ "a": ObjectId(), "b": 1 },{ "a": ObjectId(), "b": 2 }])
'[{"a": {"$oid": "55a79543268e150463d51799"}, "b": 1}, {"a": {"$oid": "55a79543268e150463d5179a"}, "b": 2}]'
And it works just like "dumps" except all the BSON type handling is built it.
Again, no need to re-invent the wheel here and "roll your own", because people already use this.
Your implementation of the JSONEncoder works well. Just use it the way it was intended to be used:
>>> JSONEncoder().encode({'data': [ObjectId(), ObjectId()]})
'{"data": ["<objId>", "<objId>"]}'
The encoder will take care of serializing dicts, objects, lists, tuples, strings (including unicode), ints, longs, floats, booleans and None. Your implementation makes it aware of ObjectIds as well. Perfect!
Just lose the string concatenation and use encode.