简体   繁体   English

生成两个JSON文件之间的值差异

[英]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). 我有一个JSON主配置文件,其值可能会被特定帐户的配置文件(同样在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. 请注意,它们是相同的,只是某些值( section01Config.setting03section02Config.setting01section03Config.setting02 )不同。 Note also that the entire section4Configs block is the same in both files. 还要注意,两个文件中的整个section4Configs块都相同。

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: 在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. 我知道OP并没有请求Python,但是如果有人遇到类似的问题并且不想被运行(甚至安装)PHP和Apache所困扰,这是一个朴素的Python实现。

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. 注意:该解决方案不保留attribute s和section的顺序。 It also requires (and generates) well-formed JSON , which looks like this: 它还需要(并生成) 格式良好的JSON ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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