diff options
author | Rasmus Luha <rasmus.luha@ut.ee> | 2025-04-06 16:46:36 +0300 |
---|---|---|
committer | Rasmus Luha <rasmus.luha@ut.ee> | 2025-04-06 16:46:36 +0300 |
commit | fc6f95665ae68c2025dd4579c6b6725d7d84a790 (patch) | |
tree | 4b03bc56978dc6f3285cc710438999eac722de2c /common | |
parent | 31de311a17c9f83d330a499ec0b5ae5f672bbb65 (diff) |
nifi json value extraction
Diffstat (limited to 'common')
-rw-r--r-- | common/core.py | 61 |
1 files changed, 53 insertions, 8 deletions
diff --git a/common/core.py b/common/core.py index 44248be..5841d41 100644 --- a/common/core.py +++ b/common/core.py @@ -10,6 +10,24 @@ def ask_binary_input(prompt="Kas jah või ei?: ", valikud=["jah","ei"]): return answer print(f"Ebakorretne sisend.Palun vasta kas '{valikud[0]}' või '{valikud[1]}'") + +def ask_digit_input(max_index): + while True: + user_input = input(f"Vali number (0 - {max_index}): ").strip() + + if not user_input.isdigit(): + print("Palun vali korrektne numer.") + continue # algusesse + + index = int(user_input) + + if 0 <= index <= max_index: + return int(index) + else: + print(f" Number ei kuulu valikusse. Palun vali number vahemikus 0-{max_index}.") + + + def is_app_url_correct(api_url, needs_auth, username,passwd): print("Teostan API kutset...\n") try: @@ -20,21 +38,48 @@ def is_app_url_correct(api_url, needs_auth, username,passwd): response.raise_for_status() ## Check if staus code is 2xx data = response.json() - print(json.dumps(data, indent=2)) - return True +# print(json.dumps(data, indent=2)) + return data, True except requests.exceptions.RequestException as e: print(f"HTTP error: {e}") - return False + return None, False except ValueError: print("andmeallikas ei tagasta vallidset JSON kuju...") - return False + return None, False except Exception as e: print(f"API kutsel tekkis viga: {e}") - return False + return None, False + + +## Func todo - add list level support with porcessors etc +def inspect_json_top_level(json_data): + while True: + print(json.dumps(json_data, indent=2)) + print("\nVali json võti millest soovid väärtuse andmekonveieriga ekstrakteerida\n") + keys = list(json_data.keys()) -##TODO -def add_api_authentication(): - print("Adding api authentication ... (TODO)") + for index, key in enumerate(keys): + value = json_data[key] + value_type = type(value).__name__ ## Mis type json itemgiga tegu + if isinstance(value, list): + suggestion = "SplitJson" + else: + suggestion = "EvaluateJsonPath" + print(f" [{index}] {key} ({value_type})") + selected_index = ask_digit_input(len(list(json_data.keys())) - 1) + selected_key = keys[selected_index] + selected_value = json_data[selected_key] + + # Wrap into new json object + #extracted = {selected_key: selected_value} + + if isinstance(selected_value, dict) or isinstance(selected_value,list): + json_data = selected_value + continue + else: + print(f"\nValitud võti: '{selected_key}':") + #print(json.dumps(selected_value, indent=2)) + return selected_key |