Reputation: 2122
I am loading JSON data in My Database but is giving me error -
TypeError - the JSON object must be str, not 'list'
from django.shortcuts import render
# Create your views here.
import json
from .models import Movie
def detail(request):
with open('movie_data.json', encoding='utf-8') as data_file:
json_data = json.loads(data_file.read())
print(type(json_data))
json_dict = json.loads(json_data)
for movie_data in json_dict:
movie = Movie.create(**movie_data)
# movie and genres created
Json file - movie_data.json
[
{
"99popularity": 83.0,
"director": "Victor Fleming",
"genre": [
"Adventure",
" Family",
" Fantasy",
" Musical"
],
"imdb_score": 8.3,
"name": "The Wizard of Oz"
},
{
"99popularity": 88.0,
"director": "George Lucas",
"genre": [
"Action",
" Adventure",
" Fantasy",
" Sci-Fi"
],
"imdb_score": 8.8,
"name": "Star Wars"
},
]
I think the json should be converted to dictionary. How can i do that. I don't know what is the problem. I am reading the json file. It should be string file?
Upvotes: 1
Views: 2793
Reputation: 28565
As stated above, no nead for json.loads()
twice. And when you do use it at json_data = json.loads(data_file.read())
, it is structured as a list of dictionaries.
Upvotes: 1
Reputation: 33275
You don't need to call json.loads()
twice. Remove the second call.
Upvotes: 4