0

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.

1
  • store all the dictionaries in a list then convert to json Commented Jan 27, 2021 at 8:24

1 Answer 1

1

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())
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response. As I'm trying to write the file to an AWS S3 bucket, open() does not work. Please see the code I'm currently using and suggest changes to it.
Isn't the open() part is equivalent to these 2 lines in your code? ``` 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()) ``` So now instead of df_domains_and_ip_addresses_to_add_to_your_allow_list.to_json(json_buffer), you do result.to_json(json_buffer) ...? You take the combined result dictionary and write it just like you did above
Thanks for checking I got this error **base is not defined
Sorry, it should be **result instead. Have made the edits

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.