简体   繁体   中英

Python - Adding JSON data of 2 files in a new file

I am trying to access 2 JSON files containing multiple amounts of data. I am checking for the 'name' key of the 2 files and if it's the same, I want to combine the specific key of 2 files and store in a new one. Please can someone help me out here?

import json


with open('file1.json') as af:
    data_a = json.load(af)

with open('file2.json') as cf:
    data_c = json.load(cf)

#FOR LOOP TO GET EACH FIRST JSON FILE KEY
for policy_a in data_a['policies'] :
    name_a = policy_a['displayName']

    #FOR LOOP TO GET EACH SECOND JSON FILE KEY
    for policy_c in data_c['rules'] :
        name_c = policy_c['properties']['displayName']

        if name_a == name_c :
            
            #Combine data_a and data_c
            #Add the specific key to new json file 

JSON File 1

{
  "policies": [
    {
      "displayName": "External accounts with owner permissions should be removed from your subscription",
      "links": null,
      "metadata": null,
      "name": "c3b6ae71-f1f0-31b4-e6c1-d5951285d03d",
      "partnersData": null,
    },
    {
      "displayName": "An activity log alert should exist for Delete SQL Server Firewall Rule",
      "links": null,
      "metadata": null,
      "name": "ea122f2e-53fc-fc5a-dc81-81bff6672b97",
      "partnersData": null,
    }
  ]
}

JSON File 2

{
  "rules": [
    {
       "properties": {
        "displayName": "External accounts with owner permissions should be removed from your subscription",
        "policyType": "BuiltIn",
        "mode": "All",
        "description1": "External accounts with owner permissions should be removed from your subscription in order to prevent unmonitored access.",
        "metadata": {
          "version": "3.0.0",
          "category": "Security Center"
        }
      } 
    }
  ]
}

For the output file, when each key of file 1 is compared to the second and a match in displayName is found, I want that key of both files in a new one as shown below in a new json file:

{
  "policies": [
    {
      "displayName": "External accounts with owner permissions should be removed from your subscription",
      "links": null,
      "metadata": null,
      "name": "c3b6ae71-f1f0-31b4-e6c1-d5951285d03d",
      "partnersData": null,
      "policyType": "BuiltIn",
      "mode": "All",
      "description1": "External accounts with owner permissions should be removed from your subscription in order to prevent unmonitored access.",
      "metadata": {
         "version": "3.0.0",
         "category": "Security Center"
      }
    }
  ]  
}

Asking the right question the right way takes you a long way towards solution. Naming things carefully is very important. Providing the sample files is a good step forwards.

Rephrasing your question: "I have a policies.json file with an object containing a list of policies and another rules.json file containing an object containing a list of rules that relate to the policies. I need to produce a new annotated_policies.json file that adds all the key, value pairs of any matching rule to each policy.

Now build your code using meaningful naming. You can load and test each function as you go to make sure it works the way you expect (use simple test data like your example files).

I've not tested the code below but it should set you on the right path.

import json


def create_annotated_policies_file(policies_filepath, rules_filepath, annotated_policies_filepath):
    policies = load_policies(policies_filepath)
    rules = load_rules(rules_filepath)
    with open(annotated_policies_filepath, 'w') as f:
        json.dump({'policies': get_annotated_policies(policies, rules)}, f)


def get_annotated_policies(policies, rules):
    return [get_annotated_policy(policy, rules) for policy in policies]


def get_annotated_policy(policy, rules):
    matching_rules = [rule for rule in rules if rule['properties']['displayName'] == policy['displayName']]
    annotated_policy = {**policy}
    for rule in matching_rules:
        annotated_policy = {**annotated_policy, **rule['properties']}


def load_rules(filepath):
    with open(filepath) as f:
        rules_object = json.load(f)
        return rules_object['rules']


def load_policies(filepath):
    with open(filepath) as f:
        policy_object = json.load(f)
        return policy_object['policies']

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM