Skip to content
The Chat Archive
sonar-reasoning-pro-high LMArena Search Arena 24K

I'm looking for code to extract json data infinite level

  1. User

    I'm looking for code to extract json data infinite level

  2. Assistant

    Extracting Data from Infinitely Nested JSON

    Working with deeply nested JSON data is a common challenge in data processing. Recursive approaches are essential for handling JSON structures of arbitrary depth. Here are comprehensive solutions in various programming languages to extract data from infinitely nested JSON structures.

    Python Solutions

    General-Purpose Recursive Function to Extract Data by Key

    def extract_by_key(obj, target_key):
        """
        Recursively search through a JSON object to find all values 
        associated with a specific key.
        
        Args:
            obj: The JSON object to search (dict, list, or primitive)
            target_key: The key to search for
        
        Returns:
            list: All values found for the target key
        """
        results = []
        
        if isinstance(obj, dict):
            for key, value in obj.items():
                if key == target_key:
                    results.append(value)
                if isinstance(value, (dict, list)):
                    results.extend(extract_by_key(value, target_key))
        elif isinstance(obj, list):
            for item in obj:
                if isinstance(item, (dict, list)):
                    results.extend(extract_by_key(item, target_key))
                    
        return results
    

    This function can extract all values associated with a specific key, no matter how deeply nested they are in the JSON structure[4][11]. It works by checking each element, adding matching key values to a results list, and recursively processing nested dictionaries and lists.

    Extract Specific Elements Based on Conditions

    def extract_conditional(obj, key_name, conditional_key, conditional_value):
        """
        Extract values for a specific key only when they're in the same object as
        another key-value pair that matches a condition.
        
        Args:
            obj: The JSON object to search
            key_name: The key whose value to extract
            conditional_key: The key to check for a condition
            conditional_value: The value that conditional_key should have
        
        Returns:
            list: Values for key_name that meet the condition
        """
        results = []
        
        if isinstance(obj, dict):
            # Check if this object meets the condition
            if conditional_key in obj and obj[conditional_key] == conditional_value:
                if key_name in obj:
                    results.append(obj[key_name])
            
            # Continue searching in all nested objects
            for value in obj.values():
                if isinstance(value, (dict, list)):
                    results.extend(extract_conditional(value, key_name, conditional_key, conditional_value))
        elif isinstance(obj, list):
            for item in obj:
                if isinstance(item, (dict, list)):
                    results.extend(extract_conditional(item, key_name, conditional_key, conditional_value))
                    
        return results
    

    This function is particularly useful when you need to extract values only when they appear in objects that satisfy certain conditions[1]. For example, extracting "text" values only when they appear alongside "type":"sentence".

    Tracking the Full Path to Keys

    def find_json_paths(obj, target_key, current_path=None):
        """
        Find all paths to a specific key in a JSON object.
        
        Args:
            obj: The JSON object to search
            target_key: The key to search for
            current_path: The current path being explored (for recursion)
        
        Returns:
            list: All paths to the target key
        """
        if current_path is None:
            current_path = []
        
        paths = []
        
        if isinstance(obj, dict):
            for key, value in obj.items():
                new_path = current_path + [key]
                
                if key == target_key:
                    paths.append(new_path)
                
                if isinstance(value, (dict, list)):
                    paths.extend(find_json_paths(value, target_key, new_path))
        elif isinstance(obj, list):
            for i, item in enumerate(obj):
                if isinstance(item, (dict, list)):
                    new_path = current_path + [i]
                    paths.extend(find_json_paths(item, target_key, new_path))
                    
        return paths
    

    This function tracks the complete path to each occurrence of a target key, which is useful when you need to know the exact location of values in the JSON structure[2].

    JavaScript Solutions

    Basic Recursive JSON Traversal

    function traverseJson(data, callback) {
        /**
         * Recursively traverse a JSON object and apply a callback to each value
         * 
         * @param {any} data - The JSON object to traverse
         * @param {Function} callback - Function to call on each value (key, value) => void
         */
        if (data && typeof data === 'object') {
            if (Array.isArray(data)) {
                for (let i = 0; i < data.length; i++) {
                    traverseJson(data[i], callback);
                }
            } else {
                for (const key in data) {
                    callback(key, data[key]);
                    traverseJson(data[key], callback);
                }
            }
        }
    }
    
    // Example usage
    const jsonData = {
        name: "John",
        age: 30,
        address: {
            street: "123 Main St",
            city: "Anytown",
            zipcode: 12345
        },
        hobbies: ["reading", "coding", {name: "gaming", frequency: "weekly"}]
    };
    
    // Print all keys and values
    traverseJson(jsonData, (key, value) => {
        if (typeof value !== 'object') {
            console.log(`${key}: ${value}`);
        }
    });
    

    This JavaScript function recursively traverses a JSON object and applies a callback function to each value[7][17]. The callback function allows you to process the data as needed during traversal.

    Searching for Specific Keys

    function findValuesByKey(obj, targetKey) {
        /**
         * Find all values associated with a specific key in a JSON object
         * 
         * @param {any} obj - The JSON object to search
         * @param {string} targetKey - The key to search for
         * @returns {Array} - Array of values found for the target key
         */
        let results = [];
        
        if (obj && typeof obj === 'object') {
            if (Array.isArray(obj)) {
                for (const item of obj) {
                    results = results.concat(findValuesByKey(item, targetKey));
                }
            } else {
                for (const key in obj) {
                    if (key === targetKey) {
                        results.push(obj[key]);
                    }
                    if (obj[key] && typeof obj[key] === 'object') {
                        results = results.concat(findValuesByKey(obj[key], targetKey));
                    }
                }
            }
        }
        
        return results;
    }
    

    This function searches for all occurrences of a specific key in a JSON object and returns an array of the associated values[17].

    Julia Implementation

    function extract(json, key)
        req = []
        for (k, v) in pairs(json)
            if k == key
                push!(req, v)
            elseif typeof(v) <: AbstractDict
                extract_result = extract(v, key)
                if !isempty(extract_result)
                    append!(req, extract_result)
                end
            end
        end
        return req
    end
    

    This Julia function recursively searches for a key in a JSON object and returns an array of all matching values[4].

    Advanced Techniques

    Handling Complex Conditions

    For more complex extraction scenarios, you might need to add additional parameters to your recursive functions:

    def extract_with_parent_context(obj, target_key, parent_keys=None, parent_context=None):
        """
        Extract values with context about their parent objects
        
        Args:
            obj: The JSON object to search
            target_key: The key to search for
            parent_keys: Keys from parent objects to include in results
            parent_context: Current parent context for recursion
            
        Returns:
            list: Dictionary items with target value and parent context
        """
        if parent_keys is None:
            parent_keys = []
        if parent_context is None:
            parent_context = {}
        
        results = []
        
        if isinstance(obj, dict):
            # Build context for this level
            current_context = parent_context.copy()
            for pk in parent_keys:
                if pk in obj:
                    current_context[pk] = obj[pk]
            
            # Check for target key
            if target_key in obj:
                results.append({
                    "value": obj[target_key],
                    "context": current_context
                })
            
            # Recurse into nested structures
            for key, value in obj.items():
                if isinstance(value, (dict, list)):
                    results.extend(extract_with_parent_context(value, target_key, parent_keys, current_context))
        
        elif isinstance(obj, list):
            for item in obj:
                if isinstance(item, (dict, list)):
                    results.extend(extract_with_parent_context(item, target_key, parent_keys, parent_context))
        
        return results
    

    This function extracts values while maintaining context about parent objects, which is useful for complex data processing tasks[6][10].

    Using a Generator for Memory Efficiency

    When working with very large JSON structures, using a generator can be more memory-efficient:

    def find_element(data, path):
        """
        Recursively traverse JSON to find elements at a specified path
        
        Args:
            data: The JSON object to search
            path: List of keys/indices defining the path to search
            
        Yields:
            Found elements
        """
        if not path:
            yield data
            return
        
        current = path
        remaining_path = path[1:]
        
        if current == "*" and isinstance(data, dict):
            # Match all keys at this level
            for value in data.values():
                yield from find_element(value, remaining_path)
        elif current == "*" and isinstance(data, list):
            # Match all items in the list
            for item in data:
                yield from find_element(item, remaining_path)
        elif isinstance(data, dict) and current in data:
            # Match specific key
            yield from find_element(data[current], remaining_path)
        elif isinstance(data, list) and isinstance(current, int) and current < len(data):
            # Match specific index
            yield from find_element(data[current], remaining_path)
    

    This generator function allows you to traverse JSON structures recursively while yielding elements as they're found, rather than building a complete result list in memory[13].

    Best Practices

    1. Type checking: Always check the type of data (dict, list, primitive) before processing to avoid errors[1][2].

    2. Base case handling: Ensure your recursive functions have appropriate base cases to prevent infinite recursion[7].

    3. Error handling: Add try-except blocks for robust error handling in production code.

    4. Performance considerations: For very large JSON structures, consider using generators or incremental processing to manage memory usage[13].

    5. Specific use cases: Adapt the recursive approach based on your specific needs (extracting by key, finding paths, etc.).

    With these recursive approaches, you can extract data from JSON structures of any complexity and nesting level, adapting the solution to your specific requirements.