简体   繁体   中英

Generate differences in values between two JSON files

I have a JSON master config file whose value may be overwritten by a specific account's config file (also in JSON).

master file

{
    "section1Configs": {
        "setting01": true,
        "setting02": true,
        "setting03": false
    },
    section2Configs: {
        "setting01": true,
        "setting02": true,
        "setting03": false
    },
    section3Configs: {
        "setting01": true,
        "setting02": true,
        "setting03": false
    },
    section4Configs: {
        "setting01": true,
        "setting02": true,
        "setting03": false
    }
}

config file

{
    "section1Configs": {
        "setting01": true,
        "setting02": true,
        "setting03": true
    },
    section2Configs: {
        "setting01": false,
        "setting02": true,
        "setting03": false
    },
    section3Configs: {
        "setting01": true,
        "setting02": false,
        "setting03": false
    },
    section4Configs: {
        "setting01": true,
        "setting02": true,
        "setting03": false
    }
}

Note that they are identical except certain values ( section01Config.setting03 , section02Config.setting01 , and section03Config.setting02 ) are different. Note also that the entire section4Configs block is the same in both files.

The ones that are the same are not needed since the application loads both and overwrites the master file with the ones that are different in the account config.

What I would like to do is have a script that iterates through a directory of such account files and deletes the entry in each file that are the same key and value as the master file. From this example I would end up with a file like this:

{
    section1Configs: {
        setting03: true
    },
    section2Configs: {
        setting01: false
    },
    section3Configs: {
        setting02: false
    }
}

Assuming the two files are well formed JSON and have always the same structure than in your example (the depth in particular), the solution is trivial:

in PHP:

$master = json_decode($master, true);
$config = json_decode($config, true);

$result = [];

foreach ($master as $ksec => $vsec) {
    $secTmp = [];
    foreach ($vsec as $kset => $vset) {
        if ($master[$ksec][$kset] !== $config[$ksec][$kset])
            $secTmp[$kset] = $config[$ksec][$kset];
    }
    if ($secTmp)
        $result[$ksec] = $secTmp;
}
echo json_encode($result);

I know that the OP didn't request Python, but just in case someone has a similiar problem and doesn't want to be bothered with running (or even installing) PHP and Apache, here is a naive Python implementation.

import json


with open('json1.json') as data_file:
    master = json.load(data_file)

with open('json2.json') as data_file:
    config = json.load(data_file)

result = {}

for section in master:
    for attribute, value in master[section].items():
        if not config[section][attribute] == value:
            result.update({section: {}})
            result[section].update({attribute: value})

with open('result.json', 'w') as f:
    json.dump(result, f, indent=4)

Notice: The solution doesn't keep the order of the attribute s nor section s. It also requires (and generates) well-formed JSON , which looks like this:

{
    "section3Configs": {
        "setting02": true
    },
    "section1Configs": {
        "setting03": false
    },
    "section2Configs": {
        "setting01": true
    }
}

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