Here's the relevant code:
print(type(document))
myjson = json.dumps( document )
Here's what I get:
<class 'dict'>
Traceback (most recent call last):
File "./monitor.py", line 965, in <module>
myjson = json.dumps( nft )
File "/usr/lib/python3.8/json/__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python3.8/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type Decimal is not JSON serializable
So type() thinks its a dict, which it is but dumps() thinks its a Decimal. Now what?
I tired assigning it to another variable thinking it was the name that was throwing things. No difference. type() thinks its a dict, dumps() thinks its a Decimal.
Related
I am not able to tell what item I touched inmy code. There is this traceback for an error. May somebody help me find the problem and how to solve it.
Traceback (most recent call last):
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\handlers\exception.py", line 47, in inner
response = get_response(request)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\utils\deprecation.py", line 119, in __call__
response = self.process_response(request, response)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\middleware.py", line 61, in process_response
request.session.save()
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\db.py", line 83, in save
obj = self.create_model_instance(data)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\db.py", line 70, in create_model_instance
session_data=self.encode(data),
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\contrib\sessions\backends\base.py", line 114, in encode
return signing.dumps(
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 110, in dumps
return TimestampSigner(key, salt=salt).sign_object(obj, serializer=serializer, compress=compress)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 172, in sign_object
data = serializer().dumps(obj)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\site-packages\django\core\signing.py", line 87, in dumps
return json.dumps(obj, separators=(',', ':')).encode('latin-1')
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\__init__.py", line 234, in dumps
return cls(
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Ptar\AppData\Local\Programs\Python\Python39\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type QuerySet is not JSON serializable
I am trying to use connexion/Flask to return a base64-encoded image generated by PIL/pillow in a HTTP Response.
Having upgraded to python3.7 from python2.7, that part of the code works unaltered.
However, when running inside Docker (python3.7-slim), I get the time-honoured TypeError: Object of type bytes is not JSON serializable (full Traceback below).
The encoding is undertaken via
cutout_b64 = base64.b64encode(sbuffer.getvalue()).decode("ascii")
PLEASE NOTE: There are many many many answers that suggest applying .decode(...) but this does not work inside Docker! The question is:
Q. Why does this work outside Docker but not inside the container? How do I fix this? Is it a LANG difference perhaps?
Thanks as ever
Traceback (most recent call last):
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1950, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1936, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/decorator.py", line 68, in wrapper
response = function(request)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/uri_parsing.py", line 149, in wrapper
response = function(request)
File "/app/custom_validators.py", line 107, in wrapper
response = function(request)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/response.py", line 110, in wrapper
return _wrapper(request, response)
File "/usr/local/lib/python3.7/site-packages/connexion/decorators/response.py", line 90, in _wrapper
self.operation.api.get_connexion_response(response, self.mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 365, in get_connexion_response
response = cls._response_from_handler(response, mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 345, in _response_from_handler
return cls._build_response(mimetype=mimetype, data=response, extra_context=extra_context)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/flask_api.py", line 186, in _build_response
data, status_code, serialized_mimetype = cls._prepare_body_and_status_code(data=data, mimetype=mimetype, status_code=status_code, extra_context=extra_context)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/abstract.py", line 417, in _prepare_body_and_status_code
body, mimetype = cls._serialize_data(data, mimetype)
File "/usr/local/lib/python3.7/site-packages/connexion/apis/flask_api.py", line 203, in _serialize_data
body = cls.jsonifier.dumps(data)
File "/usr/local/lib/python3.7/site-packages/connexion/jsonifier.py", line 57, in dumps
return self.json.dumps(data, **kwargs) + '\n'
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 211, in dumps
rv = _json.dumps(obj, **kwargs)
File "/usr/local/lib/python3.7/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/local/lib/python3.7/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/usr/local/lib/python3.7/json/encoder.py", line 431, in _iterencode
yield from _iterencode_dict(o, _current_indent_level)
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 405, in _iterencode_dict
yield from chunks
File "/usr/local/lib/python3.7/json/encoder.py", line 438, in _iterencode
o = _default(o)
File "/usr/local/lib/python3.7/site-packages/connexion/apps/flask_app.py", line 146, in default
return json.JSONEncoder.default(self, o)
File "/usr/local/lib/python3.7/site-packages/flask/json/__init__.py", line 100, in default
return _json.JSONEncoder.default(self, o)
File "/usr/local/lib/python3.7/json/encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type bytes is not JSON serializable
I am finding following error in code, Can someone please guide me why I am finding this error and how can I fix it.
I LOGGED response before its used in Response(json.dumps(response), content_type='application/json', charset='utf8'):-
[service_variant=lms][root][env:sandbox] WARNING [waleed-HP-Compaq-Elite-8300-CMT 32269] [mixins.py:75] - (json.dumps(response)-- {'url': u'/openassessment/fileupload/submissions_attachments/a419e1fff5e9a869a46d537c7ae3a18a/course-v1:edX+DemoX+Demo_Course/block-v1:edX+DemoX+Demo_Course+type#openassessment+block#051fae2bdc4f47929078eda8463a8c44/', 'success': True}
[service_variant=lms][courseware.module_render][env:sandbox] ERROR
[waleed-HP-Compaq-Elite-8300-CMT 32269] [module_render.py:1210] -
error executing xblock handler
Traceback (most recent call last):
File "/edx/app/edxapp/edx-platform/lms/djangoapps/courseware/module_render.py", line 1185, in _invoke_xblock_handler
resp = handler_instance.handle(handler, req, suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/mixins.py", line 91, in handle
return self.runtime.handle(self, handler_name, request, suffix)
File "/edx/app/edxapp/edx-platform/common/lib/xmodule/xmodule/x_module.py", line 1365, in handle
return super(MetricsMixin, self).handle(block, handler_name, request, suffix=suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/runtime.py", line 1037, in handle
results = handler(request, suffix)
File "/edx/app/edxapp/venvs/edxapp/local/lib/python2.7/site-packages/xblock/mixins.py", line 76, in wrapper
return Response(json.dumps(response), content_type='application/json', charset='utf8')
File "/usr/lib/python2.7/json/__init__.py", line 244, in dumps
return _default_encoder.encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/usr/lib/python2.7/json/encoder.py", line 184, in default
raise TypeError(repr(o) + " is not JSON serializable")
ERROR:-
TypeError: u'/openassessment/fileupload/submissions_attachments/a419e1fff5e9a869a46d537c7ae3a18a/course-v1:edX+DemoX+Demo_Course/block-v1:edX+DemoX+Demo_Course+type#openassessment+block#051fae2bdc4f47929078eda8463a8c44/' is not JSON serializable
In the main function, i call a function in which another application is called, as a result i get data in json format.
But I don’t understand where the slashes come from in front of each double quotation "
in the browser, I see data with quotes
Example: {\"192.168.43.1\":[\"53\":{\"state\":\"open\"...
If I do not send the data but write to the file, data is written to the file WITHOUT slashes
Example: {"192.168.43.1":["53":{"state":"open"...
This is normal?
How do I remove the slashes?
This data must accept
another application and deserialize them.
def get_ip(ip, port):
return os.system("some_app")
#hug.get('/scan')
def main(ip: hug.types.text, port: hug.types.text):
json = get_ip(ip, port)
#JUST FOR TEST WITH PARAM safe=False
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
Errors without parametr safe=False:
Traceback (most recent call last):
File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}")
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 552, in __init__
'In order to allow non-dict objects to be serialized set the '
TypeError: In order to allow non-dict objects to be serialized set the safe parameter to False.
Errors with parametr safe=False:
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
File "/usr/lib/python3.6/wsgiref/handlers.py", line 137, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python3.6/dist-packages/falcon/api.py", line 244, in __call__
responder(req, resp, **params)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 793, in __call__
raise exception
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 766, in __call__
self.render_content(self.call_function(input_parameters), context, request, response, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 703, in call_function
return self.interface(**parameters)
File "/usr/local/lib/python3.6/dist-packages/hug/interface.py", line 100, in __call__
return __hug_internal_self._function(*args, **kwargs)
File "script.py", line 181, in main
return JsonResponse("{\"192.168.1.1\":[\"80\":{\"state\":\"open\",\"reason\":\"syn-ack\",\"name\":\"http\",\"product\":\"\"}]}", safe=False)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 559, in __init__
super().__init__(content=data, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 291, in __init__
self.content = content
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 321, in content
content = self.make_bytes(value)
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 236, in make_bytes
return bytes(value.encode(self.charset))
File "/usr/local/lib/python3.6/dist-packages/django/http/response.py", line 85, in charset
return settings.DEFAULT_CHARSET
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 57, in __getattr__
self._setup(name)
File "/usr/local/lib/python3.6/dist-packages/django/conf/__init__.py", line 42, in _setup
% (desc, ENVIRONMENT_VARIABLE))
django.core.exceptions.ImproperlyConfigured: Requested setting
DEFAULT_CHARSET, but settings are not configured. You must either define the
environment variable DJANGO_SETTINGS_MODULE or call settings.configure()
before accessing settings.
Your return "result {json}" would return string value which contains json. Since returned value is string, browser shows you extra backslashes in order to properly handle double quotes.
To solve this problem you could handle string response in client side and extract json values from string.
JSON.parse(response);
But since another application is expecting json format you'd better to use JsonResponse to make sure return response is not string but json format
from django.http import JsonResponse
return JsonResponse(json)
I made a some code
jsonFile = json.loads(data.data, 'utf-8')
print 'jsonFile'
the result is:
{u'INFO': {u'CALL': u'000000000000', u'MODULE': u'POWERVOICE'}, u'PersonType': u'Caller', u'KEYWORD': {u'start': 720, u'end': 1920, u'value': [u'\ubc31\ud654\uc810']}, u'END_FLAG': 0, u'TEXT': {u'start': 720, u'end': 1920, u'value': u'\ub730\uc885\ud569\ubc31\ud654\uc810'}, u'FID_INFO': {u'SID': u'123456789', u'CallConfidence': u'0.123', u'FakeCall': u'1'}, u'POI': {u'start': 720, u'end': 1920, u'value': [u'']}, u'SITUATION': {u'Fire': 0, u'FirstAid': 0, u'Rescue': 0}}
but error happens
bad callback: <function resultCallback at 0x7f1ac4ce6c08>
Traceback (most recent call last):
File "/opt/ros/indigo/lib/python2.7/dist-packages/rospy/topics.py", line 720, in _invoke_callback
cb(msg)
File "/root/catkin_ws/src/listen_project/src/node_evaluate.py", line 58, in resultCallback
jsonFile = json.loads(data.data, 'utf-8')
File "/usr/lib/python2.7/json/__init__.py", line 351, in loads
return cls(encoding=encoding, **kw).decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "/usr/lib/python2.7/json/decoder.py", line 384, in raw_decode
raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
-------------------------------------
Is my json file not validate?
thank you.
You have a python literal, not a json object. (which may be loaded with ast.literal_eval)
json uses double quoted strings for everything.
It's not entirely clear from the code posted, data.data may already be a python object?
The error implies you are attempting to loads the empty string. Can you show what data.data is?