--- id: json-stringify title: "JSON Stringify" category: "Frontend" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["JSON.stringify", "JSON stringify", "object to JSON", "serialize JSON", "stringify object"] 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", "stringify"] raw_sources: ["https://www.w3schools.com/js/js_json_stringify.asp"] applied_in: [] github_commit: "" --- # [[JSON Stringify]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) When sending data to a web server the data has to be a string; `JSON.stringify()` converts any JavaScript datatype into a JSON-notation string β€” converting dates to strings and removing functions, since JSON allows neither. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **Sending data needs a string** β€” when sending data to a web server, the data has to be a string; convert any JavaScript datatype with `JSON.stringify()`. [S1] - **Works on objects and arrays** β€” the result is a string following JSON notation, ready to be sent to a server. [S1] - **Text storage** β€” text is always a legal storage format; combined with `localStorage`, JSON lets you store and reload JavaScript objects. [S1] - **Dates become strings** β€” in JSON, date objects are not allowed; `JSON.stringify()` converts any Date objects into strings (convert back at the receiver). [S1] - **Functions are removed** β€” in JSON, functions are not allowed as object values; `JSON.stringify()` removes any functions (both the key and the value). [S1] ## 🧩 μΆ”μΆœλœ νŒ¨ν„΄ (Extracted patterns) - **Serialize-then-send** β€” `JSON.stringify(obj)` produces a transmittable string. [S1] - **Round-trip storage** β€” `JSON.stringify` to save in `localStorage`, `JSON.parse` to read it back. [S1] - **Pre-stringify a function to keep it** β€” call `obj.fn = obj.fn.toString()` before stringifying so the function survives as a string. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) A common use of JSON is to exchange data to/from a web server. When sending data to a web server, the data has to be a string. You can convert any JavaScript datatype into a string with `JSON.stringify()`. [S1] **Stringify a JavaScript object** ```javascript const obj = {name: "John", age: 30, city: "New York"}; const myJSON = JSON.stringify(obj); ``` The result will be a string following the JSON notation. `myJSON` is now a string, and ready to be sent to a server. [S1] **Stringify a JavaScript array** ```javascript const arr = ["John", "Peter", "Sally", "Jane"]; const myJSON = JSON.stringify(arr); ``` The result will be a string following the JSON notation. `myJSON` is now a string, and ready to be sent to a server. [S1] **Storing data** When storing data, the data has to be a certain format, and regardless of where you choose to store it, text is always one of the legal formats. JSON makes it possible to store JavaScript objects as text. Example storing and retrieving data in local storage: [S1] ```javascript const myObj = {name: "John", age: 31, city: "New York"}; const myJSON = JSON.stringify(myObj); localStorage.setItem("testJSON", myJSON); let text = localStorage.getItem("testJSON"); let obj = JSON.parse(text); document.getElementById("demo").innerHTML = obj.name; ``` **Stringify a number** [S1] ```javascript const num = 123e-5; const myJSON = JSON.stringify(num); ``` **Stringify a boolean** [S1] ```javascript let bool = new Boolean(1); const myJSON = JSON.stringify(bool); ``` **Stringify a date** In JSON, date objects are not allowed. The `JSON.stringify()` function will convert any Date objects into strings. [S1] ```javascript const obj = {name: "John", today: new Date(), city : "New York"}; const myJSON = JSON.stringify(obj); ``` You can convert the string back into a date object at the receiver. [S1] **Stringify a function** In JSON, functions are not allowed as object values. The `JSON.stringify()` function will remove any functions from a JavaScript object, both the key and the value. [S1] ```javascript const obj = {name: "John", age: function () {return 30;}, city: "New York"}; const myJSON = JSON.stringify(obj); ``` You can preserve a function by converting it to a string before stringifying: [S1] ```javascript const obj = {name: "John", age: function () {return 30;}, city: "New York"}; obj.age = obj.age.toString(); const myJSON = JSON.stringify(obj); ``` If you send functions using JSON, the functions will lose their scope, and the receiver would have to use `eval()` to convert them back into functions. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) Applied examples on the page: serialize an object and an array for sending to a server; round-trip an object through `localStorage`; stringify numbers, booleans, dates, and functions, including the `.toString()` trick to keep a function. No external project/commit applications found in the source. ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Serialize an object to send to a server: ```javascript const obj = {name: "John", age: 30, city: "New York"}; const myJSON = JSON.stringify(obj); ``` Round-trip an object through local storage: ```javascript const myJSON = JSON.stringify(myObj); localStorage.setItem("testJSON", myJSON); let obj = JSON.parse(localStorage.getItem("testJSON")); ``` ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (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 Parse]], [[JavaScript JSON]], [[JavaScript JSON Data Types]], [[JavaScript JSON Objects]] - **μ°Έμ‘° λ§₯락:** Referenced whenever serializing JavaScript data for transmission to a server or storage. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” JSON Stringify β€” https://www.w3schools.com/js/js_json_stringify.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-06-23: Initial draft synthesized from the W3Schools "JSON Stringify" page (Astra wiki-curation, P-Reinforce v3.1 format).