1

I have file.json that contains

{"list" : [

]}

I have this dict

lead = {'title': i.css('article>.sales-info>h1::text').extract_first(),
                    'address': i.css('article>.primary-info>.contact>h2::text').extract_first(),
                    'phone': i.css('article>.primary-info>.contact>.phone::text').extract_first(),
                    'time': i.css('article>.primary-info>.contact>.time-info>div:nth-child(2)::text').extract_first(),
                    'website': i.css('.business-card-footer>.website-link::attr(href)').extract_first(),
                    'email': i.css('.business-card-footer>.email-business::attr(href)').extract_first(),
                    'portfolio_item': ''} 

I need to append this dict to the json list. I've tried basic writing to file

with open('leads.json', 'a') as f:
    f.write(json.dumps(item))
    f.close()

Have no idea how should i append it to the json list

8
  • you need to load the json file to a dict first then access the <data>['list] attribute of the json and append that then rewrite the json. Do you have any other data at the root of your json? or is it just the list of leads? Commented May 11, 2020 at 3:16
  • @TenaciousB I've thought of that, but the issue is that the lead dict is passed on trough the system, basically i would need to load the list every time a new lead item is generated. It's a back and forward process that would waste resources and would be very unprofessional. Commented May 11, 2020 at 3:23
  • you want to append a list to existing json file right? so are you using this json file as sort of a data store? Commented May 11, 2020 at 3:28
  • @TenaciousB no, i want to append a dict to existing list in json file. And yes i'm using it as temp datastore Commented May 11, 2020 at 3:29
  • is your json format JUST a list of leads or is there other data at the root of the json file other than the "list" array? Commented May 11, 2020 at 3:31

1 Answer 1

1

I think this could work for you:

EDIT: forgot to add the write_json() function

def write_json(path, data, indent=4):
    with open(path, 'w') as file: 
        json.dump(data, file, indent=indent) 

def append_json(path, data, key=None):
    if not os.path.exists(path):
        write_json(path, data)
    else:
        with open(path) as file:
            # load a dict of the json data
            json_data = json.load(file)
            # key should be a list of objects so you can __add__ them
            if key is None:
                json_data += data
            else:
                json_data[key] += data
            write_json(path, json_data)

appen_json('./leads.json', [lead], 'list') # you need to pass in a list, not just dict

The way this is written requires you to pass in a list rather than a dictionary but that has an advantage because you can pass in multiple items to append rather than one-at-a-time

This will write a new file if one doesn't exist in the director OR append if it does exist

Sign up to request clarification or add additional context in comments.

1 Comment

Hope it works for you! I have been building up a list of my most frequent data file operations here: gist.github.com/bherbruck/04bd208725accccdc7f0bbabad474a51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.