Search code examples
pythonjsonlistdictionarylist-manipulation

How can i create multiple json payload efficiently by reading input from sys.argv in python


Problem statement:

This is how I am invoking my prepare_payload.py ,

python3 prepare_payload.py ['Test_B1','Test_B2','Test_B3'] [https://10.5.5.1,https://10.5.5.2,https://10.5.5.3] ['abc','efg','sss']

my json payload which I am trying to prepare:

{
    "billing_account_number": "",
    "vcenter_url": "",
    "cred_header": "",
}

Expected output:

{
    "billing_account_number": "Test_B1",
    "vcenter_url": "https://10.5.5.1",
    "cred_header": "abc",
}

{
    "billing_account_number": "Test_B2",
    "vcenter_url": "https://10.5.5.2",
    "cred_header": "efg",
}
{
    "billing_account_number": "Test_B3",
    "vcenter_url": "https://10.5.5.3",
    "cred_header": "sss",
}

my code:

import json
import os
import sys

master_list = []
billing_account_number = sys.argv[1]
ip_addr = sys.argv[2]
cred_header = sys.argv[3]
res = list(map(str, billing_account_number.strip('[]').split(',')))
ip = list(map(str, ip_addr.strip('[]').split(',')))
cred_headers = list(map(str, cred_header.strip('[]').split(',')))
master_list.append(res)
master_list.append(ip)
master_list.append(cred_headers)
def prepare_payload():
    with open("rabbitmq_payload.json") as fh:
        data = json.load(fh)
    print('================================================')
    return data
data = prepare_payload()
for i in master_list:
    for j in i:
        data['billing_account_number'] = j
        data['vcenter_url'] = j
        data['cred_header'] = j
        
print(data)

I am not able to figure if I have to merge these individual list such as res, IP, cred_headers into a single list and then iterate like main list [res[0],IP[0],cred_headers[0]] and then start replacing key value pair in my data dictionary? Please help me if there is any built in function I can rely on or any efficient approach to solve this problem. Thank you in advance for all the awesome python coders!


Solution

  • It's kind of inconvenient to pass in lists as command line arguments. Better to use standard input, but nonetheless, take a look at this code:

    import sys
    import ast
    
    billing_account_number = ast.literal_eval(sys.argv[1])
    ip_addr = ast.literal_eval(sys.argv[2])
    cred_header = ast.literal_eval(sys.argv[3])
    output = []
    for i in range(len(billing_account_number)):
        output.append({"billing_account_number": billing_account_number[i], "ip_addr": ip_addr[i], "cred_header": cred_header[i]})
    print(output)
    

    The output:

    [{'billing_account_number': 'Test_B1', 'ip_addr': 'https://10.5.5.1', 'cred_header': 'abc'}, {'billing_account_number': 'Test_B2', 'ip_addr': 'https://10.5.5.2', 'cred_header': 'efg'}, {'billing_account_number': 'Test_B3', 'ip_addr': 'https://10.5.5.3', 'cred_header': 'sss'}]
    

    You will need to wrap your command line arguments in quotes so that it parses correctly. Also, this code assumes that all 3 lists are the same length. If that's not guaranteed, then you'll need to iterate over the longest list and set the missing values to empty string.