{
    "slug": "js_json_parse_stringify",
    "term": "JSON.parse & JSON.stringify",
    "category": "javascript",
    "difficulty": "beginner",
    "short": "JSON.stringify converts JS objects to JSON strings; JSON.parse converts them back — direct counterpart to PHP's json_encode/json_decode.",
    "long": "JSON.stringify(value, replacer, space) — replacer filters/transforms keys, space pretty-prints. JSON.parse(text, reviver) — reviver transforms parsed values (e.g. date string to Date object). Both throw on invalid input. Circular references throw in stringify. undefined, functions, and Symbol values are omitted from stringified output. PHP json_decode($str, true) returns associative array; without true returns stdClass — know which you're consuming in JavaScript.",
    "aliases": [
        "JSON stringify",
        "JSON parse",
        "JSON serialisation",
        "json_encode counterpart"
    ],
    "tags": [
        "javascript",
        "php-integration",
        "json"
    ],
    "misconception": "JSON.parse is always safe to call on server responses — always wrap in try/catch; PHP error pages and redirects return HTML not JSON, and JSON.parse on HTML throws SyntaxError.",
    "why_it_matters": "Every PHP JSON API response must be parsed, and every request body must be stringified — understanding the edge cases prevents silent data loss and runtime errors.",
    "common_mistakes": [
        "Not wrapping JSON.parse in try/catch",
        "Losing Date objects (serialised as strings, not revived automatically)",
        "undefined values silently dropped by JSON.stringify",
        "Circular reference crash in JSON.stringify"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "js_fetch_api",
        "js_ajax_patterns",
        "js_error_types"
    ],
    "prerequisites": [
        "js_error_types"
    ],
    "refs": [
        "https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON"
    ],
    "bad_code": "// Silent failures:\nconst data = JSON.parse(serverResponse); // Crashes on HTML error page\nconst payload = JSON.stringify({ id: 1, fn: () => {} }); // fn silently dropped",
    "good_code": "// Safe parse with fallback:\nlet data;\ntry {\n    data = JSON.parse(serverResponse);\n} catch (e) {\n    console.error('Non-JSON response:', serverResponse.slice(0, 200));\n    throw new Error('Server returned unexpected format');\n}\n\n// Replacer to control output:\nconst payload = JSON.stringify(obj, (key, val) =>\n    typeof val === 'function' ? undefined : val\n, 2);",
    "quick_fix": "Always wrap JSON.parse in try/catch — PHP can return HTML error pages when JSON is expected",
    "severity": "medium",
    "effort": "low",
    "created": "2026-03-17",
    "updated": "2026-03-22",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/js_json_parse_stringify",
        "html_url": "https://codeclaritylab.com/glossary/js_json_parse_stringify",
        "json_url": "https://codeclaritylab.com/glossary/js_json_parse_stringify.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[JSON.parse & JSON.stringify](https://codeclaritylab.com/glossary/js_json_parse_stringify) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/js_json_parse_stringify"
            }
        }
    }
}