Home > Net >  Django - Data import from JSON file URL into Database
Django - Data import from JSON file URL into Database

Time:12-13

I'm trying to import data from a json file URL into my Django Database on a weekly basis to keep my Database up to date. That Data is coming from an Importer which was written in GO and which provides me with that JSON File via a link.

I still don't have a way to automate this or to make sure how to check if an item got updated or not and then only update those items. But that is not my main issue right now. My main issue is that i'm receiving the following error every time i try to import the data with the manage.py command:

File "\data_import\management\commands\import_from_url.py", line 45, in handle
self.import_facility_from_file()
File "\data_import\management\commands\import_from_url.py", line 21, in import_facility_from_file
Name = data_object.get('Name', None)
AttributeError: 'str' object has no attribute 'get'

This is my Code:

import os
import json
from data_import.models import Facility
from django.core.management.base import BaseCommand
from datetime import datetime
from jsontest.settings import BASE_DIR, STATIC_URL


class Command(BaseCommand):
    def import_facility_from_file(self):
        print(BASE_DIR)
        data_folder = os.path.join(BASE_DIR, 'import_data', 'resources')

        for data_file in os.listdir(data_folder):
            with open(os.path.join(data_folder, data_file), encoding='utf-8') as data_file:
                data = json.loads(data_file.read())
                for data_object in data:
                    Name = data_object.get('Name', None)
                    IssuedNumber = data_object.get('IssuedNumber', None)
                    release_year = datetime.now()

                    try:
                        Facility, created = Facility.objects.get_or_create(
                            Name=Name,
                            IssuedNumber=IssuedNumber,
                            release_year=release_year
                        )
                        if created:
                            Facility.save()
                            display_format = "\Facility, {}, has been saved."
                            print(display_format.format(Facility))
                    except Exception as ex:
                        print(str(ex))
                        msg = "\n\nSomething went wrong saving this Facility: {}\n{}".format(Name, str(ex))
                        print(msg)


    def handle(self, *args, **options):
        """
        Call the function to import data
        """
        self.import_facility_from_file()

The JSON looks like this:

{"00016ed7be4d872aseddsf6f36bfsdfd647f3eb41cadedd2130bdfsdfsdf6fbbbf24c2f1a64d2cf34ac4e03aaa30309816f98a7389b2bb324a2":{"UUID":"00016ed7be4d872aseddsf6f36bfsdfd647f3eb41cadedd2130bdfsdfsdf6fbbbf24c2f1a64d2cf34ac4e03aaa30309816f98a7389b2bb324a2","Name":"SENIOR CARE","IssuedNumber":"123456","Licensee":"SENIOR CARE","Email":"[email protected]","AdministratorName":"Tester","TelephoneNumber":"(123) 456-789101112","AddressInfo":{"PrimaryAddress":"123 Test Road","SecondaryAddress":"","City":"Testcity","RegionOrState":"TESTSTATE","PostalCode":"12345","Geolocation":"11.1111,-11.1111"},"Capacity":1,"MostRecentLicenseTimestamp":1234567891,"ClosedTimestamp":0,"InspectionInfo":{"ComplaintRelatedVisits":0,"InspectionRelatedVisits":0,"NumberOfVisits":0,"LastVisitTimestamp":0},"Complaints":{"ComplaintsTypeA":0,"ComplaintsTypeB":0,"SubstantiatedAllegations":0,"TotalAllegations":0}},

CodePudding user response:

You're treating an str object like a dictionary:

AttributeError: 'str' object has no attribute 'get'

Why does this happen? When you iterate over the JSON, the outer object is a dictionary:

data = json.loads(data_file.read())

(If you come from a JavaScript background, a dictionary is like an object.)

Then, you iterate over the dictionary:

for data_object in data:

When you iterate over a dictionary, you iterate over its keys. data_object is a string, because the keys of this dictionary are strings.

Then you call .get() on the string:

Name = data_object.get('Name', None)

That is only defined for dictionaries, and not strings.

What you want to do is to iterate over the values of the dictionary. How do you do this?

data = json.loads(data_file.read())
for data_object in data.values():

Or, if you want the keys and values at the same time:

data = json.loads(data_file.read())
for key, data_object in data.items():
  • Related