There are two json files, the first is:
{ "First": { "Key_1": 0, "Key_2": [1,34] } } the second has the form:
{ "Key_1": [0,1], "Key_2": [1,100] } I want to make the program read the first file, take "Key_1", find it in the second file, and take values from the range that is specified in the second file. The program below shows an example of a simple key search.
path = Path('first.json') P=Path('second.json') data = json.loads(path.read_text(encoding='utf-8')) Second=json.loads(P.read_text(encoding='utf-8')) for KEY, VALUE in Second.items(): for firstKey, firstValue in data.items(): for key, value in firstValue.items(): I do not know how to do this:
for example, in the first file we take "Key_1", we look that its value is "0", it means that in the new, the resulting file must be constant. Go to the second file, find "Key_1" there, we see that the value of it is the range "[0,1]", but since in the first file the value of "Key_1" is constant, then we need to choose a constant from this range, or 0, or 1. Next, we take "Key_2", we see that in the first file its value is the range "[1.34]", which means that in the new, resulting file there must be a range. We go to the second file, we find there "Key_2", we see that the value of it is the range "[1,100]", how to make so that the new range is generated? The program should take and generate two new numbers from the range "[1,100]", the first number is min, the second is max, as a result there should be something like this [[min, max] ".
That is, the program must understand that it has values in the first file, a constant or a range, and based on this act
at the same time, it’s not an option to prescribe fields and values
data_gen = { "frame": { "Key_1": lambda: randint(0, 1), "Key_2": lambda: [randint(1, 100) for i in range(2)] } } I could write it this way, but my json can consist of thousands of lines, and it’s impossible to set everything up with my hands, I need to automate it somehow.
At the pseudo-code level, I see something like this
if(firstValue[value] == Const): firstValue[key] = random(VALUE) if(firstValue[value] == [min,max]): new_min=random([min,max]) new_max=random([min,max]) firstValue[value] == [new_min,new_max] And the result should be some kind of json
{ "First": { "Key_1": 1, "Key_2": [12,57] } }