-1

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
    }
}
sjaustirni
  • 3,056
  • 7
  • 32
  • 50
vdiaz1130
  • 301
  • 1
  • 3
  • 13
  • Do you have access to any other programming language, like perl? – pooley1994 Jul 24 '15 at 15:39
  • I don't know Perl but can make the effort to get it running. So if you have a suggestion with Perl, that be good as well. – vdiaz1130 Jul 24 '15 at 15:51
  • Not all of your keys are [well-formed](http://stackoverflow.com/questions/949449/json-spec-does-the-key-have-to-be-surrounded-with-quotes) (they miss quotes). Is it just a mistake, or your files don't have them everywhere? – sjaustirni Jul 24 '15 at 17:30

2 Answers2

2

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);
Casimir et Hippolyte
  • 88,009
  • 5
  • 94
  • 125
1

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 attributes nor sections. It also requires (and generates) well-formed JSON, which looks like this:

{
    "section3Configs": {
        "setting02": true
    },
    "section1Configs": {
        "setting03": false
    },
    "section2Configs": {
        "setting01": true
    }
}
Community
  • 1
  • 1
sjaustirni
  • 3,056
  • 7
  • 32
  • 50