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] } } 
  • only whole and couples, I can't figure it out yet - Jikukoda
  • I can't figure out how to do all this, syntax knowledge is not enough - Jikukoda

1 answer 1

The basic structure of the code might look like this :

 #!/usr/bin/env python3 import json from pathlib import Path data = json.loads(Path('first.json').read_text(encoding='utf-8')) library = json.loads(Path('second.json').read_text(encoding='utf-8')) for d in data.values(): for key in (library.keys() & d.keys()): d[key] = generate(d[key], library[key]) print(json.dumps(data)) 

where the generate() function creates a new value:

 def generate(current_value, possible_range): if isinstance(current_value, int): # scalar return random.randint(*possible_range) else: # range assert isinstance(current_value, list) and len(current_value) == 2 return sorted(random.randint(*possible_range) for i in range(2)) 

If there are more types, then it is possible to split into several functions, for example, using functools.singledispatch() or a dictionary with functions ( funcs[type(v).__name__](*args) ) or getattr() on an object by part of the method name in depending on the type ( getattr(builder, 'make_' + type(v).__name__)(*args) ), and so on.

 <script src="https://cdn.rawgit.com/brython-dev/brython/3.4.0/www/src/brython.js"></script><script src="https://cdn.rawgit.com/brython-dev/brython/3.4.0/www/src/brython_stdlib.js"></script><body onload="brython()"><script type="text/python"> import json import random def generate(current_value, possible_range): if isinstance(current_value, int): return random.randint(*possible_range) else: assert isinstance(current_value, list) and len(current_value) == 2 return sorted(random.randint(*possible_range) for i in range(2)) # try your own input from browser import document, html @document["mybutton"].bind("click") def on_click(event): library = json.loads(document['library'].value) data = json.loads(document['data'].value) for d in data.values(): for key in (library.keys() & d.keys()): d[key] = generate(d[key], library[key]) document['output'] <= html.PRE(json.dumps(data)) </script> <label for="library">library: </label><textarea id="library" rows=12 cols=25>{ "Key_1": [0,1], "Key_2": [1,100] }</textarea><label for="data">data: </label><textarea id="data" rows=12 cols=25>{ "First": { "Key_1": 0, "Key_2": [1,34] } }</textarea><button id="mybutton">Запустить</button><div id=output /></body>