Derpa
Derpa

Reputation: 27

for loop dict written to json

I've got a script that pulls information from an API. The API is a database for job postings and I receive the information as json.

I then give the different titles, names, dates a variable. The variables get updated by a for loop and printed one after another so I'll end up with a post like this:

ID: 234523
jobtitle
company
deadline
link

ID: 5454554
jobtitle
company
deadline
link

etc.

What I want now is to output this to a json file, so that I later can compare the ID's and add new postings to the file. The problem I have at the moment is that the formatting is off in the database.json file after outputting it like this:

for i in jobs:
    employername = i['employer']
    emp_type = i['employment_type']
    fulltime = i['duration']
    jobtitle = i['headline']
    etc.


    output = {
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    }
    with open('pbdb.json', 'a+') as job_data_file:
        json.dump(output, job_data_file, indent=4,)

The output would be something like:

{
    "ID": "23961983",
    "Title": "Test",
    "Employer": "comp",
    "Employment type": "regular",
    "Fulltime": "fulltime",
    "Deadline": "2020-09-06",
    "Link": "https://"
}{
    "ID": "23960352",
    "Title": "a job",
    "Employer": "comp2",
    "Employment type": "regular",
    "Fulltime": "4 months",
    "Deadline": "2020-03-27",
    "Link": "https://"
}

and I'd get errors in the json file, no commas between the dictionaries and "End of file expected." is there a better way to do this?

Upvotes: 0

Views: 62

Answers (1)

rassar
rassar

Reputation: 5660

You need to dump it as a list, not as individual entries:

output = []
for i in jobs:
    ...

    output.append({
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    })

with open('pbdb.json', 'w') as job_data_file:
    json.dump(output, job_data_file)

To make it append to the json file:

output = json.load(open("pbdb.json"))

Breaks if the file is empty, workaround:

import os

check_empty = os.stat('pbdb.json').st_size
if check_empty == 0:
    with open('pbdb.json', 'w') as f:
        f.write('[\n]')    # Writes '[' then linebreaks with '\n' and writes ']'
output = json.load(open("pbdb.json"))

for i in jobs:
    output.append({
        'ID': job_id, 
        'Title': jobtitle, 
        'Employer' : company, 
        'Employment type' : emptype, 
        'Fulltime' : tid, 
        'Deadline' : deadline, 
        'Link' : webpage
    })

with open('pbdb.json', 'w') as job_data_file:
    json.dump(output, job_data_file)

Upvotes: 1

Related Questions