Reputation: 2975
I am trying to write eight JSON dictionaries each similar to the below-shown format into a single JSON file.
{
"Category": {
"0": "PCoIP Session Gateway (PSG)",
"1": "Session Broker (PCM)",
"2": "Web Access TURN Servers for PCoIP"
},
"Domain or IP address": {
"0": "PCoIP Gateway Servers",
"1": "Domains: https://skylight-cm.us-east-1.amazonaws.com https://skylight-cm-fips.us-east-1.amazonaws.com https://skylight-cm.us-west-2.amazonaws.com https://skylight-cm-fips.us-west-2.amazonaws.com https://skylight-cm.ap-northeast-2.amazonaws.com https://skylight-cm.ap-southeast-1.amazonaws.com https://skylight-cm.ap-southeast-2.amazonaws.com https://skylight-cm.ap-northeast-1.amazonaws.com https://skylight-cm.ca-central-1.amazonaws.com https://skylight-cm.eu-central-1.amazonaws.com https://skylight-cm.eu-west-1.amazonaws.com https://skylight-cm.eu-west-2.amazonaws.com https://skylight-cm.sa-east-1.amazonaws.com https://skylight-cm.us-gov-west-1.amazonaws.com https://skylight-cm-fips.us-gov-west-1.amazonaws.com",
"2": "Servers: turn:*.us-east-1.rdn.amazonaws.com turn:*.us-west-2.rdn.amazonaws.com turn:*.ap-northeast-2.rdn.amazonaws.com turn:*.ap-southeast-1.rdn.amazonaws.com turn:*.ap-southeast-2.rdn.amazonaws.com turn:*.ap-northeast-1.rdn.amazonaws.com turn:*.ca-central-1.rdn.amazonaws.com turn:*.eu-central-1.rdn.amazonaws.com turn:*.eu-west-1.rdn.amazonaws.com turn:*.eu-west-2.rdn.amazonaws.com turn:*.sa-east-1.rdn.amazonaws.com"
}
}
This is my current code -
def export_to_json(df_domains_and_ip_addresses_to_add_to_your_allow_list \
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_pcoip \
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_workSpaces_streaming_protocol_wsp_beta \
, df_health_check_servers \
, df_pcoip_gateway_servers \
, df_ip_ranges \
, df_wsp_beta_gateway_servers \
, df_management_interface_ip_ranges):
dest_file_name = 'WORKSPACES_PORT_REQ_' + str(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.json'
json_buffer = io.StringIO()
# Adding dict to json file
df_domains_and_ip_addresses_to_add_to_your_allow_list.to_json(json_buffer)
my_bucket.put_object(Key=dest_file_name, Body=json_buffer.getvalue())
This code currently adds one dictionary to the JSON file, how do I add all the other dictionaries (passed to this function as parameters) to the same JSON file.
Upvotes: 0
Views: 123
Reputation: 115
Do you want the output file to be in the format {...}
(contents of all files combined into one dictionary), or [{...}, {...} ...]
(each file is a list element)?
Depending on your goal:
import json
# --- FORMAT: {...} --- #
result1 = {}
# your input dictionaries
for dct in dicts:
# combine the dictionaries
# if there are duplicate keys, the ones in 'dct' will overwrite those already in 'result'
result1 = {**result1, **dct}
# write the output file
with open('result1.json', 'w') as outfile:
json.dump(result1 , outfile, indent=2)
# --- FORMAT: [{...}, {...} ...] --- #
result2 = []
# your input dictionaries
for dct in dicts:
# append the dictionary to the list
result2.append(dct)
# write the output file
with open('result2.json', 'w') as outfile:
json.dump(result2, outfile, indent=2)
The above gives you two different output files depending on the intended format.
EDIT: To get your input dfs in a list, you can use locals().keys()
in your function. So you can do something like for df in locals().keys(): ...
Putting it all together:
def export_to_json(df_domains_and_ip_addresses_to_add_to_your_allow_list \
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_pcoip \
, df_domains_and_ip_addresses_to_add_to_your_allow_list_for_workSpaces_streaming_protocol_wsp_beta \
, df_health_check_servers \
, df_pcoip_gateway_servers \
, df_ip_ranges \
, df_wsp_beta_gateway_servers \
, df_management_interface_ip_ranges):
dest_file_name = 'WORKSPACES_PORT_REQ_' + str(datetime.datetime.now().strftime('%Y_%m_%d_%H_%M_%S')) + '.json'
json_buffer = io.StringIO()
result = {}
dfs = locals().keys()
for df in dfs:
result = {**result, **df}
# Adding dict to json file
result.to_json(json_buffer)
my_bucket.put_object(Key=dest_file_name, Body=json_buffer.getvalue())
Upvotes: 1