ERPNext Foundation ERPNext Cloud User Manual Blog Discuss Frappé* Donate

Bug: Frappe API encoding case "TypeError: the JSON object must be str"



Hello everyone , I found a bug in but I’m not sure is it bug or not.

I have created API with PHP which is connected with Frappe/ERPNEXT ,I tried to insert new lead but I got error

Traceback (most recent call last):\n File “/home/frappe/frappe-bench/apps/frappe/frappe/”, line 66, in application\n response = frappe.api.handle()\n File “/home/frappe/frappe-bench/apps/frappe/frappe/”, line 117, in handle\n data = json.loads(\n File “/usr/lib/python3.5/json/”, line 312, in loads\n\nTypeError: the JSON object must be str, not ‘bytes’\n

Firstly I thought it’s about my request, I checked with different post fields but i had always same error.

what i tried with curl :

curl_setopt($ch, CURLOPT_POSTFIELDS, array('data' => json_encode($params['data'])));
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($params['data']));
curl_setopt($ch, CURLOPT_POSTFIELDS, '{"lead_name":"Sercan Tarakçı","email_id":"Sercan Tarakçı","source":"Campaign"}');

but it didnt work. Then i think that maybe it’s in , I opened line 117

if frappe.local.request.method=="POST":
                                        data = json.loads(
                                                "doctype": doctype
                                                "data": frappe.get_doc(data).insert().as_dict()

I changed this line
data = json.loads(
with this simple trick:
data = json.loads('utf-8'))

all my request works really fine now.Can someone explain is problem in my curl request or in python code?

Can't generate stock balance report

I got a same error too but from Stock Projected Qty report page.

Traceback (most recent call last):
  File "/home/frappe/bench/apps/frappe/frappe/", line 61, in application
	response = frappe.handler.handle()
  File "/home/frappe/bench/apps/frappe/frappe/", line 21, in handle
	data = execute_cmd(cmd)
  File "/home/frappe/bench/apps/frappe/frappe/", line 56, in execute_cmd
	return, **frappe.form_dict)
  File "/home/frappe/bench/apps/frappe/frappe/", line 1007, in call
	return fn(*args, **newargs)
  File "/home/frappe/bench/apps/frappe/frappe/", line 489, in wrapper_fn
	retval = fn(*args, **get_newargs(fn, kwargs))
  File "/home/frappe/bench/apps/frappe/frappe/desk/", line 177, in run
	result = get_prepared_report_result(report, filters, dn, user)
  File "/home/frappe/bench/apps/frappe/frappe/desk/", line 203, in get_prepared_report_result
	data = json.loads(uncompressed_content)
  File "/usr/lib/python3.5/json/", line 312, in loads
TypeError: the JSON object must be str, not 'bytes'

After adding .decode('utf-8') to json.loads(uncompressed_content) it work.

I’m using Python 3 with
ERPNext: v11.0.3-beta.31 () (staging)
Frappe Framework: v11.0.3-beta.40 () (staging)

Anyone knows what might be the problem here?


So I was working on something like this recently and the problem with the JSON encoding was the there were no new lines ("\n") after each comma - it has to be valid JSON and the new line is part of that. Encoded:


And the header:

I’m not sure if this was your specific issue or not, but it was what got me over the hump.


The problem is Python 3.x

The JSON section of this article (near the bottom) gets into better detail, but this same fix worked for me.

Before this can be an accepted fix, some testing has to be done that it doesn’t break on 2.7.


I too encountered this error. It appears when i run query report having no data. i.e. on fresh instance, but once it has some data which appears as part of report. the said error goes away