--- id: json-parse title: "JSON Parse" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JSON.parse", "JSON parse", "parse JSON", "JSON to object", "reviver function"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-06-23 updated_at: 2026-06-23 review_reason: "" merge_history: [] tags: ["javascript", "js", "web", "frontend", "w3schools", "json", "parse"] raw_sources: ["https://www.w3schools.com/js/js_json_parse.asp"] applied_in: [] github_commit: "" --- # [[JSON Parse]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) When data arrives from a web server it is always a string; `JSON.parse()` turns that JSON string into a usable JavaScript object (or array), and dates and functions β€” which JSON cannot carry β€” must be reconstructed by hand or via a reviver. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Server data is always a string** β€” a common use of JSON is to exchange data to/from a web server; when receiving it, parse with `JSON.parse()` and the data becomes a JavaScript object. [S1] - **Text must be valid JSON** β€” make sure the text is in JSON format, or you will get a syntax error. [S1] - **Arrays parse to arrays** β€” using `JSON.parse()` on JSON derived from an array returns a JavaScript array, not an object. [S1] - **Dates are not allowed in JSON** β€” store a date as a string, then convert it back to a Date object later (directly or with a reviver function). [S1] - **Functions are not allowed in JSON** β€” avoid them; reconstructing requires `eval()`, and functions lose their scope. [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Receive β†’ parse β†’ render** β€” `JSON.parse(text)` then read fields into the page. [S1] - **Reviver pattern** β€” pass a function as the second argument to `JSON.parse()` to transform values (e.g. convert a `"birth"` string into a Date) during parsing. [S1] - **Manual rehydration** β€” after parsing, replace a string field with a real object: `obj.birth = new Date(obj.birth)`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) A common use of JSON is to exchange data to/from a web server. When receiving data from a web server, the data is always a string. Parse the data with `JSON.parse()`, and the data becomes a JavaScript object. [S1] **Example β€” parsing JSON** Imagine we received this text from a web server: `'{"name":"John", "age":30, "city":"New York"}'`. Use `JSON.parse()` to convert the text into a JavaScript object: [S1] ```javascript const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); ``` Make sure the text is in JSON format, or else you will get a syntax error. Then use the object in your page: [S1] ```javascript

``` **Array as JSON** When using `JSON.parse()` on a JSON derived from an array, the method will return a JavaScript array, instead of a JavaScript object: [S1] ```javascript const text = '["Ford", "BMW", "Audi", "Fiat"]'; const myArr = JSON.parse(text); ``` **Exceptions β€” parsing dates** Date objects are not allowed in JSON. If you need to include a date, write it as a string. You can convert it back into a date object later. Convert directly after parsing: [S1] ```javascript const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}'; const obj = JSON.parse(text); obj.birth = new Date(obj.birth); document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth; ``` Or use the second parameter of `JSON.parse()`, called the reviver function, which is called on each value before returning it: [S1] ```javascript const text = '{"name":"John", "birth":"1986-12-14", "city":"New York"}'; const obj = JSON.parse(text, function (key, value) { if (key == "birth") { return new Date(value); } else { return value; } }); document.getElementById("demo").innerHTML = obj.name + ", " + obj.birth; ``` **Exceptions β€” parsing functions** Functions are not allowed in JSON. If you need to include a function, write it as a string and convert it back into a function later: [S1] ```javascript const text = '{"name":"John", "age":"function () {return 30;}", "city":"New York"}'; const obj = JSON.parse(text); obj.age = eval("(" + obj.age + ")"); document.getElementById("demo").innerHTML = obj.name + ", " + obj.age(); ``` You should avoid using functions in JSON; the functions will lose their scope, and you would have to use `eval()` to convert them back into functions. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Applied examples on the page: parse server text into `obj` and render `obj.name` into `#demo`; parse a JSON array into a real array; rehydrate a date both manually and via a reviver; and (discouraged) rebuild a function with `eval()`. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Parse server text into an object: ```javascript const obj = JSON.parse('{"name":"John", "age":30, "city":"New York"}'); document.getElementById("demo").innerHTML = obj.name; ``` Reviver to reconstruct a date during parse: ```javascript const obj = JSON.parse(text, function (key, value) { if (key == "birth") { return new Date(value); } else { return value; } }); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) No contradictions found in the source. ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual (μ‹€μ œ 적용 사둀 발견 μ‹œ applied/validated둜 승격 κ°€λŠ₯) - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[JavaScript Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[JavaScript JSON Stringify]], [[JavaScript JSON]], [[JavaScript JSON Data Types]], [[JavaScript JSON Objects]] - **μ°Έμ‘° λ§₯락:** Referenced whenever consuming JSON received from a server or storage and turning it into usable objects. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JSON Parse β€” https://www.w3schools.com/js/js_json_parse.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JSON Parse" page (Astra wiki-curation, P-Reinforce v3.1 format).