Skip to content
TheChatArchiveThe Chat Archive
gpt-4-1106-preview· OpenAIAi2 WildChat-4.8M (AI2)

I'm tyring to recursively search thoruhg a json object in JS. I need to know if the next…

  1. User

    I'm tyring to recursively search thoruhg a json object in JS. I need to know if the next object is a JSON object and thus I need to pass it into the recursive function for searching, and I need to ensure it's not a string or int or anything that isn't a JSON object

  2. Assistant

    In JavaScript, to check if something is a plain object (which could be equivalent to a JSON object), and not a string, number, or another type, you can use the typeof operator and Object.prototype.toString.call() method. Here's a function that will recursively search through a JSON-like object:

    function isObject(value) {
        return Object.prototype.toString.call(value) === '[object Object]';
    }
    
    function recursiveSearch(obj, searchTerm) {
        if (isObject(obj)) {
            for (const key in obj) {
                if (isObject(obj[key])) {
                    // If it's a nested object, search within it recursively
                    recursiveSearch(obj[key], searchTerm);
                } else if (obj[key] === searchTerm) {
                    // If the value is equal to the search term, you've found a match
                    console.log('Found:', obj[key]);
                    // Depending on what you want to do when you find the match,
                    // you might return here, store the result, or just log it
                }
            }
        }
    }
    
    // Example usage
    const jsonObject = {
        a: 1,
        b: "text",
        c: {
            d: {
                e: 2
            },
            f: "searchMe"
        }
    };
    
    recursiveSearch(jsonObject, "searchMe"); // This should log 'Found: searchMe'
    

    Note that this function does not handle arrays. If your JSON-like object contains arrays, you'd need to account for that in your recursive function. Here's a revised version that works for both objects and arrays:

    function isObject(value) {
        return Object.prototype.toString.call(value) === '[object Object]';
    }
    
    function isArray(value) {
        return Array.isArray(value);
    }
    
    function recursiveSearch(obj, searchTerm) {
        if (isObject(obj) || isArray(obj)) {
            for (const key in obj) {
                if (isObject(obj[key]) || isArray(obj[key])) {
                    // If it's a nested object or an array, search within it recursively
                    recursiveSearch(obj[key], searchTerm);
                } else if (obj[key] === searchTerm) {
                    // If the value is equal to the search term, you've found a match
                    console.log('Found:', obj[key]);
                    // Take action as necessary when you find a match
                }
            }
        }
    }
    
    // Example usage
    const jsonObject = {
        a: 1,
        b: "text",
        c: {
            d: {
                e: 2
            },
            f: "searchMe",
            g: ["searchMe", "notThisOne"]
        }
    };
    
    recursiveSearch(jsonObject, "searchMe"); // This should log 'Found: searchMe' twice
    

    Make sure to convert your JSON string into a JavaScript object using JSON.parse() before using the recursiveSearch function if it's not already an object.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.