unable to load data in django models form a json file

156 Views Asked by At

I am trying to upload JSON file data in my model using pandas but it is causing an error in loading the data

here is my model in which i want to load data

class Jsondata(models.Model):
    region =  models.CharField(max_length=50, blank=True)
    start_year =  models.IntegerField()
    published = models.CharField(max_length=100, blank=True)
    country = models.CharField(max_length=100, blank=True)
    
    def __str_(self):
        return self.country

here is my load_data.py file

import json
import pandas as pd
from django.core.management.base import BaseCommand
from users.models import Jsondata

class Command(BaseCommand):
    help = 'Load data from JSON file using pandas'

def add_arguments(self, parser):
    parser.add_argument('json_file', type=str)

def handle(self, *args, **kwargs):
    json_file_path = kwargs['json_file']

    with open(json_file_path, 'r') as json_file:
        data = json.load(json_file)
    
    # Create a DataFrame from JSON data
    df = pd.DataFrame(data)
    
    # Iterate through DataFrame rows and populate the database
    for index, row in df.iterrows():
        item = Jsondata(
                region=row['region'],
                start_year=row['start_year'],
                published=row['published'],
                country=row['country'],

        )
        item.save()
        
    self.stdout.write(self.style.SUCCESS('Data loaded successfully'))

and i tried this command to load data in my data

python manage.py load_data media\json_files\jsondata.json

and my json file look like

       {
            "region": "Central America",
            "start_year": "",
            "published": "January, 18 2017 00:00:00",
            "country": "Mexico",
        },

any other method to achieve so will also be helpful

1

There are 1 best solutions below

4
Ahmad Othman On

First of all, you should fix your JSON because it has a trailing comma and JSON does not allow trailing commas. It should be looking like this:

{
    "region": "Central America",
    "start_year": "",
    "published": "January, 18 2017 00:00:00",
    "country": "Mexico"
}

Second, you need to ensure that your published field is being parsed as a datetime object.

Modify your code like this:

df['published'] = pd.to_datetime(df['published'])


Third, you should allow blank values for the start_year field in your model

Modify your code like this:

start_year = models.CharField(max_length=10, blank=True)


You can also use the built-in loaddata command to load the JSON directly into the database without using pandas. This will use Django's serializer to load the data into your model.

Make sure that your JSON file is located in a location that Django can find and you would also have to remove the trailing commas from your JSON.

This is what I can offer you since you did not provide the exact error that you have been facing.