Created
April 9, 2024 15:26
-
-
Save Flogex/188fd384137efbc32815632fe1a7ae25 to your computer and use it in GitHub Desktop.
Generate JSON file with unique object keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, string, math | |
MAX_LEVEL = 6 | |
def randomword(): | |
letters = string.ascii_lowercase | |
return ''.join(random.choice(letters) for i in range(5)) | |
chars = ["A", "B", "C", "D"] | |
def write_json(key, level): | |
if (key): | |
print(f"\"{key}\":") | |
print("{") | |
if level == MAX_LEVEL: | |
rand_int = random.randrange(1000) | |
rand_float = math.ceil(random.uniform(1.0, 9.99) * 100) / 100 | |
rand_string_0 = randomword() | |
rand_string_1 = randomword() | |
rand_string_2 = randomword() | |
rand_bool = "true" if random.randrange(2) == 1 else "false" | |
print(f"\"{key}_0\": {rand_int},") | |
print(f"\"{key}_1\": {rand_float},") | |
print(f"\"{key}_2\": \"{rand_string_0}\",") | |
print(f"\"{key}_3\": \"{rand_string_1}\",") | |
print(f"\"{key}_4\": \"{rand_string_1}\",") | |
print(f"\"{key}_5\": {rand_bool}") | |
else: | |
for c in chars: | |
write_json(key + c, level + 1) | |
print("}") | |
if (key and key[-1] != "D") : | |
print(",") | |
if __name__ == "__main__": | |
write_json("", 0) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment