Jak formatovat JSON jako profesional

Kompletni pruvodce formatovanim JSON: pravidla syntaxe, caste chyby, osvedcene postupy a priklady z praxe.

Co je JSON a proc zalezi na formatovani

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. Proper formatting -- also called "pretty-printing" -- adds indentation, line breaks, and consistent spacing that makes JSON far more readable than a minified single-line string.

Pravidla syntaxe JSON

  • Key-value pairs: Data is stored as "key": value pairs
  • Strings: Always use double quotes ("hello"), never single quotes
  • Numbers: No quotes around numbers (42, not "42")
  • Booleans: Lowercase true and false (not True)
  • Null: Lowercase null (not None or nil)
  • Arrays: Square brackets [1, 2, 3]
  • Objects: Curly braces {"key": "value"}
  • Commas: Separate items with commas, but no trailing comma after the last item

Caste chyby v JSON a jak je opravit

  • Single quotes: JSON requires double quotes. {'key': 'value'} is invalid. Fix: {"key": "value"}
  • Trailing commas: {"a": 1,} is invalid. Remove the last comma.
  • Comments: JSON does not support // or /* */ comments. Remove them.
  • Unquoted keys: {key: "value"} is invalid. JSON requires {"key": "value"}
  • NaN/Infinity: Not valid JSON. Use null or a string representation.

Priklady z praxe

Here is a well-formatted JSON object representing a user profile:

{
  "name": "Jane Doe",
  "email": "jane@example.com",
  "age": 30,
  "active": true,
  "roles": ["admin", "editor"],
  "address": {
    "city": "Prague",
    "country": "Czech Republic"
  },
  "preferences": null
}

Use our JSON Formatter tool to instantly beautify any minified JSON.

Osvedcene postupy pro praci s JSON

  • Always validate JSON before using it in production
  • Use 2-space indentation for readability
  • Keep JSON files under 1MB for browser performance
  • Use consistent key naming (camelCase or snake_case)
  • Consider JSON Schema for complex data validation

Často kladené dotazy

Jaký je rozdíl mezi JSON a objekty JavaScript?
JSON je textový datový formát s přísnými pravidly (pouze dvojité uvozovky, žádné funkce, žádné komentáře). Objekty JavaScript jsou flexibilnější a mohou obsahovat funkce, hodnoty undefined a řetězce v jednoduchých uvozovkách.
Může JSON obsahovat komentáře?
Ne. Standardní JSON (RFC 8259) komentáře nepodporuje. Některé parsery je přijímají jako rozšíření (JSONC), ale nejde o standard a řada nástrojů s tím nebude umět pracovat.
Jaká je maximální velikost souboru JSON?
Oficiální limit neexistuje, ale prohlížeče běžně zvládnou soubory JSON do velikosti 30–50 MB, než se výkon začne zhoršovat. U větších datových sad zvažte streamovací parsery.
Jak minifikovat JSON?
Odstraňte všechny bílé znaky, konce řádků a odsazení. Snižíte tím velikost souboru přenášeného po síti. Náš nástroj JSON Formatter umí JSON zkrášlit i minifikovat.
Rozlišuje JSON velikost písmen?
Ano. "Name" a "name" jsou v JSON zcela odlišné klíče. Vždy používejte konzistentní velikost písmen.