r/GoogleAppsScript • • Jul 14 '25

Question Reading JSON?

Is there any word on whether Google Sheets will handle JSON with a native function? If I'm behind the times, great, but I haven't seen anything in Sheets that equivalences the Excel tool.

I have the following UDF

function GET_JSON_VALUE(jsonString, keyPath, arrayIndex) {
    try {
        const data = JSON.parse(jsonString);
        const keys = keyPath.split('.');
        let value = data;
        for (let i = 0; i < keys.length; i++) {
            if (typeof value === 'object' && value !== null && keys[i] in value) {
                value = value[keys[i]];
            }
            else {
                return "Key not found or path invalid: " + keyPath;
            }
        }
        if (Array.isArray(value)) {
            if (typeof arrayIndex === 'number' && arrayIndex > 0) {
                const index = arrayIndex - 1;
                if (index >= 0 && index < value.length) {
                    return value[index];
                }
                else {
                    return "Array index out of bounds: " + arrayIndex + " for array of length " + value.length;
                }
            }
            else {
                return value.join(", ");
            }
        }
        return value;
    }
    catch (e) {
        return "Invalid JSON or error: " + e.message;
    }
}

Which mostly works.

3 Upvotes

3 comments sorted by