How to convert uploaded file (json) to python code object json ?

I have a form in my template where I upload a json file.

But I can’t figure out how to convert it into a python object in the code.

I try different ways to do it, but it doesn’t work for me.

> def file_upload_teplo(request):
>     if request.method == "POST":
>         form = UploadFileForm(request.POST, request.FILES)
>         file = request.FILES['file']
>         print(
>         json_data = file
>         print(json_data)
>         # data =
>         # data = {'doc_file':}
>         # result = json.dumps(json_data, cls=MyJsonEncoder)
>         json_new = json.load(json_data)
>         print(type(json_new))
>         print(json_new)
>     else:
>         form = UploadFileForm()
>     return render(request, "file_upload_teplo.html", {'form': form})
>     <div class="row">
>         <div class="col-6">
>             <form action="{% url 'file_upload_teplo' %}" method="POST" enctype="multipart/form-data">
>                 {% csrf_token %}
>                 <p>
>                     {{form.as_p}}
>                 </p>
>                 <p>
>                     <input type="submit" value="Upload" />
>                 </p>
>             </form>
>         </div>
>     </div>
> class UploadFileForm(forms.Form):
>     file = forms.FileField()

Side note: When posting code, enclose the code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted, avoiding the need for you to add the > characters at the beginning of each line. (Note: Those lines of ``` must be lines by themselves, there must not be anything else before or after those backticks on that line.)

You’re really quite close.

The first question would be whether or not you want to keep the file as a file. From what you’re showing here, I’m going to guess that answer to be a no.

You have:

the second line here is unnecessary. You’ve defined file as a field on the form, so form.file is your handle to that file object.

As an uploaded file, you need to read the data from the file to processes it. Your commented code shows you were on the right track, but because of the duplication of functionality among those lines, I can’t tell exactly what you tried at any point to identify a particular line needing to be fixed.

Thanks a lot for the information!

Seems to have figured it out.

The only thing that does not work out very well is the conversion of the required amount of data from the json object to the DataFrame object.
I need to take only the first element from the json object.
Is it possible to do this somehow using the index - in the event that the (name-name) of the field column of the first element is not known in advance?

json_new = json.load(json_data)
df = pd.json_normalize(json_new[“first_element_field”])

json_new = json.load(json_data)
df = pd.json_normalize(json_new[“1 (index)”])

I’m not following what you’re asking for here. Please be more specific by what you mean of the “first element”. (Perhaps a minimal example would help.)

Keep in mind that JSON dicts are not ordered. There’s no assurance or guarantee that the json string "{'d': 1, 'a': 2}" is going to yield 'd' as the “first” element of that dict.