2

I want to append multiple objects into a single array in a JSON file. Each object is created after executing the code and then saved into an array in a JSON file.

I have this code:

import json
users = [
    {
        "username": "",
        "phone": ""
    }
]

username = input('Username: ')
phone = input('Phone: ')
for user in users:
    user['username'] = username
    user['phone'] = phone

with open('users.json', 'a') as file:
    json.dump(users, file, indent=4)

After executing this code once, I get this:

[
    {
        "username": "Mark",
        "phone": "333-4743"
    }
]

After executing twice I get this:

[
    {
        "username": "Mark",
        "phone": "333-4743"
    }
][
    {
        "username": "Jane",
        "phone": "555-6723"
    }
]

But I want this result:

[
    {
        "username": "Mark",
        "phone": "333-4743"
    },
    {
        "username": "Jane",
        "phone": "555-6723"
    }
]

How can I achieve this result?

2
  • 2
    You're putting a new array each time you append, You need to parse the json in the .json file each time and get the array to append to it if it exists Commented Mar 29, 2020 at 0:58
  • Please clarify what exactly the issue is. See How to Ask, help center. Commented Mar 29, 2020 at 1:19

3 Answers 3

2

You can use:

import json
from os import path

users = [
    {
        "username": "",
        "phone": ""
    }
]

username = input('Username: ')
phone = input('Phone: ')
for user in users:
    user['username'] = username
    user['phone'] = phone
    
my_path = 'users.json'
if path.exists(my_path):
    with open(my_path , 'r') as file:
        previous_json = json.load(file)
        users = previous_json + users
        
with open(my_path , 'w') as file:
    json.dump(users, file, indent=4)

In your code, you are just appending every entry to your file on each run of your code. To have only one list with all your entries, you have to first read the previous entries and then add the new entry.

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

Comments

2

Instead of appending to the file each time, you should parse the JSON and use .append() on the list:

import json, os

if not os.path.exists("users.json"):
    with open("users.json", "w") as f:
        f.write("[]")


users = json.load(open("users.json"))

username = input('Username: ')
phone = input('Phone: ')


users.append({
    "username": username,
    "phone":phone
})

with open('users.json', 'w') as file:
    json.dump(users, file, indent=4)

Comments

0

You need to just use append() method of list to add new object to your list

import json

#load code goes here, running from path
# where user.json file is located

With open('user.json', 'a') as file:
    User=json.loads(file)
    username = input('Username: ')
    phone = input('Phone: ')
    Data = {'Username': username,'phone': phone}
    users.append(Data)
    Json.dump(User,file,indent=4)

1 Comment

json != Json - module names are case sensitive.

Your Answer

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