File size: 1,814 Bytes
42cd5f6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import json


def is_valid_json(json_string):
    try:
        json.loads(json_string)
        return True
    except json.JSONDecodeError as e:
        print("JSONDecodeError:", e)
        return False


def get_json_keys_as_string(json_string):
    try:
        # Load the JSON string into a Python object
        json_data = json.loads(json_string)

        # If the input is a list, treat it like a dictionary by merging all the keys
        if isinstance(json_data, list):
            merged_dict = {}
            for item in json_data:
                if isinstance(item, dict):
                    merged_dict.update(item)
            json_data = merged_dict  # Now json_data is a dictionary

        # A helper function to recursively gather keys while preserving order
        def extract_keys(data, keys):
            if isinstance(data, dict):
                for key, value in data.items():
                    if isinstance(value, dict):
                        # Recursively extract from nested dictionaries
                        extract_keys(value, keys)
                    elif isinstance(value, list):
                        # Process each dictionary inside the list
                        for item in value:
                            if isinstance(item, dict):
                                extract_keys(item, keys)
                    else:
                        if key not in keys:
                            keys.append(key)
            return keys

        # List to hold the keys in order
        keys = []

        # Process the top-level dictionary first
        extract_keys(json_data, keys)

        # Join and return the keys as a comma-separated string
        return ', '.join(keys)

    except json.JSONDecodeError:
        print("Invalid JSON string.")
        return ''