I'm trying to create a set of JSON configuration files for my program. I generally pass a JSON file to the command line and run my simulation. The issue is that I want to explore a lot parameters and I want to create a config files for each set of parameters. For instance, if my config file looks like this:
{
"A": x,
"B": "Green",
"C": {
"c_a": "O2",
"c_b": y
}
}
}
I'd like to iterate over a set of values for each key, for instance x = [1, 2, 3]
, and y = [5, 6, 7]
. I'd like to have a solution that user can define all parameters on top and JSONnet produce all combinations in different files.
I'm a bit familiar with the JSONnet and I know that I can have functions and basically pass different values for each key and have a new json file. But this is not very scalable, and my config file is nested which makes everything more complicated, also, this method would not really address the permutation of parameters.
I've this resolved with a shell script generating a table of permutations, and then passing each line to jsonnet as an input, but I think there should be a better way.
Edit: if there is a way to achieve this using JSONnet Python binding, that'd be ok too.
Do you mean something like:
import json
import itertools
params_info = {
"x": [ 1, 2, 3],
"y": [ "a", "b", "c"],
"z": [ "A", "B", "C"],
}
for param_vals in itertools.product(*params_info.values()):
params = dict(zip(params_info.keys(), param_vals))
data = {
"A": params["x"],
"B": "Green",
"C": {
"c_a": "O2",
"c_b": params["y"],
"c_c": ["D", "E", "F", params["z"]]
}
}
jsonstr = json.dumps(data) # use json.dump if you want to dump to a file
print(jsonstr)
# add code here to do something with json