【问题标题】:Generate differences in values between two JSON files生成两个 JSON 文件之间的值差异
【发布时间】:2015-10-15 08:34:42
【问题描述】:

我有一个 JSON 主配置文件,其值可能会被特定帐户的配置文件(也在 JSON 中)覆盖。

主文件

{
    "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
    }
}

配置文件

{
    "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
    }
}

请注意,除了某些值(section01Config.setting03section02Config.setting01section03Config.setting02)不同外,它们是相同的。另请注意,两个文件中的整个 section4Configs 块是相同的。

不需要相同的文件,因为应用程序会加载两者并用帐户配置中不同的文件覆盖主文件。

我想要做的是有一个脚本遍历此类帐户文件的目录并删除每个文件中与主文件相同的键和值的条目。在这个例子中,我最终会得到一个像这样的文件:

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

【问题讨论】:

  • 您是否可以使用任何其他编程语言,例如 perl?
  • 我不懂 Perl,但可以努力让它运行起来。因此,如果您对 Perl 有建议,那也很好。
  • 并非所有密钥都是well-formed(它们缺少引号)。这只是一个错误,还是您的文件没有无处不在?

标签: php json bash perl


【解决方案1】:

假设这两个文件是格式良好的 JSON,并且始终具有与您的示例相同的结构(特别是深度),则解决方案很简单:

在 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);

【讨论】:

  • 对于了解 PHP 的人来说,这很简单;)这足以让我继续前进。谢谢!
【解决方案2】:

我知道 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)

注意:该解决方案不保持attributes 和sections 的顺序。它还需要(并生成)well-formed JSON,如下所示:

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

【讨论】:

    猜你喜欢
    • 2023-04-04
    • 2017-02-14
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-08
    • 1970-01-01
    相关资源
    最近更新 更多