Home > Software design >  add element in json object working with 2 different lists
add element in json object working with 2 different lists

Time:05-04

i am trying to add an email object to the testing accounts json. finding myself a little stuck on this.

ideally i would like to add an email field to the testing_accounts objects. 1 Per.

jsondata

testing_accounts = [{'tenantName': 'hsmd1012159', 'studentName': 'stu1012159', 'studentPassword': 'asdf123!'}, {'tenantName': 'hsmd1012716', 'studentName': 'stu1012716', 'studentPassword': 'asdf123!'}, {'tenantName': 'hsmd1011234', 'studentName': 'stu1011234', 'studentPassword': 'asdf123!'}]

item i would like to add to each json object above.

test = "[email protected]\[email protected]\[email protected]"

The other catch is if there are not enough emails to match the tenants then add a "[email protected]"

Desired output

desiredOutput = [{
                   'tenantName': 'hsmd1012159', 'studentName': 'stu1012159', 'studentPassword': 'asdf123!', 'email': '[email protected]'
                 }, 
                 {
                   'tenantName': 'hsmd1012716', 'studentName': 'stu1012716', 'studentPassword': 'asdf123!', 'email': '[email protected]'
                 },
                 {
                   'tenantName': 'hsmd1011234', 'studentName': 'stu1011234', 'studentPassword': 'asdf123!' , 'email': '[email protected]'
                  },
                 {
                   'tenantName': 'hsmd1011s2234', 'studentName': 'stu122011234', 'studentPassword': 'asdf123!' , 'email': 'null@null'
                 }

]

Where i am at so far....

print(len(testing_accounts))

print(testing_accounts)

def newline(string):
    listRes = list(string.split("\n"))
    return listRes
def comma(string):
    listRes = list(string.split(","))
    return listRes
def comma_and_space(string):
    listRes = list(string.split(", "))
    return listRes


if '\n' in test:
    test1 = newline(test)
    test1 = json.dumps(test1)
    print(test1)
elif ',' in test:
    test1 = comma(test)
    test1 = json.dumps(test1)
    print(test1)
elif ', ' in test:
    test1 = comma_and_space(test)
    test1 = json.dumps(test1)
    print(test1)
else:
    print("didn't hit any of those")
print(len(test1))
print(test1)
for item in testing_accounts:
    for email in test1:
        item.update({'email': email})
    #temp = json.loads(testing_accounts)
    #temp.update({'email': test})
    print(item)

any guidance would be greatly appreciated.

CodePudding user response:

Try itertools.zip_longest:

from itertools import zip_longest

test = "[email protected]\[email protected]\[email protected]"

for email, account in zip_longest(
    test.split(), testing_accounts, fillvalue="[email protected]"
):
    account["email"] = email

print(testing_accounts)

Prints:

[
    {
        "tenantName": "hsmd1012159",
        "studentName": "stu1012159",
        "studentPassword": "asdf123!",
        "email": "[email protected]",
    },
    {
        "tenantName": "hsmd1012716",
        "studentName": "stu1012716",
        "studentPassword": "asdf123!",
        "email": "[email protected]",
    },
    {
        "tenantName": "hsmd1011234",
        "studentName": "stu1011234",
        "studentPassword": "asdf123!",
        "email": "[email protected]",
    },
    {
        "tenantName": "hsmd1011s2234",
        "studentName": "stu122011234",
        "studentPassword": "asdf123!",
        "email": "[email protected]",
    },
]
  • Related