-1

I got "AttributeError: 'NoneType' object has no attribute 'to_json'" when I tried to make a function that would give output in JSON file using Jupyter notebook. Here the code:

import json
def ma2(request):
    request_json=json.loads(request)
    nf=main_stat.func(request_json['variable1'],request_json['variable2'],request_json['variable3'])
    return nf.to_json(orient="records")

the output:

AttributeError   
Traceback (most recent call last)
<ipython-input-3-75b494d5499e> in <module>
  9    "variable3":"Chemicals"}
 10 request=json.dumps(k)
 11 print(main(request))

<ipython-input-3-75b494d5499e> in ma2(request)
  3     request_json=json.loads(request)
  4    nf=main_stat.func(request_json['variable1'],request_json['variable2'],request_json['variable3'])
  5     return nf.to_json(orient="records")
  6 

AttributeError: 'NoneType' object has no attribute 'to_json'

njzk2
  • 38,969
  • 7
  • 69
  • 107
  • This error means that the previous function call returned None, so when you try to call to_json() on that it can't work. – Dan R Jan 05 '21 at 22:09

1 Answers1

0

You didn't mention main_stat class but as i can see func function at main_stat class returns None, that doesn't have to_json function.

Check if statements that returns None value. or if you mean it your code could have empty json you must check nt value if it's not None then use to_json function.

import json
def ma2(request):
    request_json=json.loads(request)
    nf=main_stat.func(request_json['variable1'],request_json['variable2'],request_json['variable3'])
    return nf.to_json(orient="records") if nt else {}
Purya
  • 123
  • 7