JSON Stringify

JSON को ऐसी string में बदलें जिसे code, URL या text field में आसानी से paste किया जा सके, और जरूरत पड़ने पर फिर JSON में वापस लाएं।

संबंधित सुझाव

JSON Stringify और JSON को string के रूप में embed करना क्या है?

JSON Stringify वह प्रक्रिया है जिसमें JSON object या array को string representation में बदला जाता है। JavaScript में इसके लिए आम तौर पर `JSON.stringify()` का उपयोग किया जाता है।

वास्तविक काम में यह output अक्सर source code, database text field, URL parameter या किसी दूसरे JSON field के string value के रूप में embed करनी पड़ती है। तब quotes, newline और backslash को एक अतिरिक्त layer में escape करना जरूरी होता है।

यह टूल standard stringify करने के बाद outer quotes और internal escaping अपने आप जोड़ देता है। Parse mode इसके उलट काम करके escaped string को पढ़ने योग्य JSON में वापस बदल देता है। पूरा काम browser में local रूप से होता है।

उपयोग के मामले

  • JSON को JavaScript / Java / Python / Go के लिए string literal में बदलना
  • API response JSON को किसी दूसरे JSON field में string value के रूप में embed करना
  • JSON string को database text column या Redis value में store करना
  • JSON को URL query parameter या header के जरिए भेजना
  • log या config files से escaped JSON को readable JSON में वापस लाना

उपयोग कैसे करें

  1. बाईं तरफ JSON या string paste करें, या sample / file upload का उपयोग करें
  2. Stringify या Parse mode चुनें और जरूरत हो तो output format बदलें
  3. दाईं तरफ result देखें और उसे code, document या config में copy करें
  4. ज़रूरत पड़ने पर `stringified.txt` या `parsed.json` download करें

विशेषताएं

  • दोनों दिशा में conversion: Stringify और Parse दोनों modes उपलब्ध हैं
  • ऑटोमैटिक escaping: quotes, backslash, newline और tab को सुरक्षित रूप से handle करता है
  • अलग-अलग output formats: quoted string, formatted JSON या compact single-line output
  • Code embedding के लिए तैयार: JavaScript, Java, Python और Go में paste करना आसान
  • URL और database scenarios के लिए उपयोगी: JSON को text के रूप में store या pass करना आसान बनाता है
  • पूरी तरह local processing: serialization और restoration browser के अंदर ही होती है

कोड उदाहरण

JavaScript: JSON को embeddable string में बदलना

javascript
const data = { name: 'Alice', age: 30, tags: ['dev', 'writer'] };
const json = JSON.stringify(data, null, 2);
const embedded = '"' + json
  .replace(/\\/g, '\\\\')
  .replace(/"/g, '\\"')
  .replace(/\n/g, '\\n')
  .replace(/\r/g, '\\r')
  .replace(/\t/g, '\\t') + '"';

Python: json.dumps से दोबारा escape करना

python
import json

data = {"name": "Alice", "age": 30}
pretty = json.dumps(data, indent=2, ensure_ascii=False)
embedded = json.dumps(pretty)
restored = json.loads(json.loads(embedded))

jq: JSON file को string में बदलना

bash
jq -Rs . data.json

echo '"{\"name\":\"Alice\"}"' | jq -r .

सामान्य प्रश्न

क्या JSON Stringify और JSON.stringify() एक ही हैं?

Base serialization logic एक जैसा है, लेकिन यह टूल embed करने के लिए outer quotes और internal escape भी जोड़ता है।

यह JSON Escape से कैसे अलग है?

JSON Stringify पूरे JSON object पर काम करता है, जबकि JSON Escape केवल plain string के special characters पर।

क्या Stringify के बाद JSON पढ़ना मुश्किल हो जाएगा?

नहीं। Pretty mode में line breaks और indentation बने रहते हैं, इसलिए output अभी भी readable रहता है।

क्या Parse mode `\n` और `\"` को restore कर सकता है?

हाँ। अगर input सही तरह से escaped है, तो टूल outer quotes हटाकर special characters को वापस restore कर देगा।

क्या result को सीधे URL में डाल सकते हैं?

JSON level पर सुरक्षित है, लेकिन URL के लिए `encodeURIComponent` जैसी अतिरिक्त encoding बेहतर रहती है।

क्या nested JSON के लिए भी उपयोगी है?

हाँ। आप inner JSON को string बनाकर outer JSON में string value के रूप में रख सकते हैं।

क्या result को file में download कर सकते हैं?

हाँ। Stringify mode `stringified.txt` और Parse mode `parsed.json` के रूप में download देता है।

क्या मेरा JSON server पर भेजा जाता है?

नहीं। सब कुछ local चलता है, इसलिए sensitive JSON के लिए भी यह ज्यादा सुरक्षित है।

समस्या निवारण

Stringify में Invalid JSON error आ रहा है

अक्सर trailing comma, single quotes, बिना quotes वाले keys या comments इसका कारण होते हैं।

Parse के बाद result JSON नहीं बन रहा

हो सकता है outer quotes गायब हों या backslash sequence टूट गई हो। Input को दोबारा जांचें।

Code में paste करने पर SyntaxError आता है

Quotes और backslash को हाथ से बदलने पर output आसानी से टूट सकता है। Tool का result जस का तस उपयोग करना बेहतर है।

बड़े JSON से browser धीमा हो जाता है

कई MB पर memory usage बढ़ जाती है। बड़े inputs के लिए Node.js या Python बेहतर रहेगा।

हिंदी या emoji `\uXXXX` में नहीं बदलते

यह सामान्य है। JSON सीधे Unicode characters रख सकता है। अगर `\uXXXX` चाहिए तो output के बाद extra processing करें।

शब्दकोश

JSON.stringify
JavaScript का built-in method जो JS value को JSON string में serialize करता है।
JSON.parse
JavaScript का built-in method जो JSON string को object या array में बदलता है।
Serialize
Memory में मौजूद data structure को store या transfer योग्य text format में बदलने की प्रक्रिया।
Deserialize
String से data structure को वापस restore करने की प्रक्रिया।
Escape
Special characters के आगे backslash जोड़ना ताकि outer syntax न टूटे।
String Literal
Source code में quotes के भीतर लिखी गई स्थिर string value।
Double-Escape
पहले से escaped string को दूसरी layer में embed करते समय फिर से escape करना।
URL Encode
URL के reserved characters को `%XX` में बदलना; यह JSON escape से अलग step है।

JSON Stringify character escape table

मूल characterStringify के बादनामनोट
"\"डबल quoteouter string को बंद होने से रोकने के लिए जरूरी
\\\Backslashescape character खुद भी double होता है
नई पंक्ति\nNewlinemulti-line content को एक string में रखने के लिए जरूरी
Tab\tTabcontrol character escape
CR\rCarriage ReturnWindows line ending का हिस्सा
//Slashआमतौर पर ऐसे ही रहने दिया जा सकता है

JSON.stringify और इस टूल में अंतर

टूलइनपुटआउटपुटसामान्य उपयोग
JSON.stringify()JS objectstandard JSON stringAPI, file save, normal serialization
इस टूल का Stringify modeJSON objectouter quotes और escape के साथ embeddable stringcode, DB, URL, nested JSON
इस टूल का Parse modeescaped stringstandard JSON objectlog या database से JSON restore करना
JSON Escapeplain textसिर्फ special chars escaped stringसाधारण text को सुरक्षित बनाना

Authoritative References