Was ist JSON? Anfänger-Guide mit Syntax, Beispielen
JSON (JavaScript Object Notation) ist ein Text-Format für Daten-Austausch — Keys, Values, Arrays, Objects. Universal in APIs, Configs, NoSQL-Datenbanken.
Was ist JSON?
JSON (JavaScript Object Notation) ist ein leichtgewichtiges, text-basiertes Daten-Format zur Repräsentation von strukturierten Daten. Trotz des Namens ist es language-agnostisch — jede moderne Programmiersprache hat Libraries, um es zu parsen und zu generieren.
JSON-Syntax-Basics
JSON hat sechs Datentypen:
- Object: Sammlung von Key-Value-Paaren in
{} - Array: Ordered List in
[] - String: Text in
"double quotes" - Number: Integer oder Float
- Boolean:
trueoderfalse - null
JSON-Beispiel
{
"id": 42,
"name": "Alice Johnson",
"email": "alice@example.com",
"active": true,
"preferences": {
"language": "en",
"notifications": false
},
"tags": ["admin", "beta-tester"]
}JSON-Syntax-Regeln
- Keys müssen Strings sein, in Double-Quotes
- Strings müssen Double-Quotes nutzen
- Keine Trailing Commas
- Keine Comments
- Kein undefined; null nutzen
Häufige JSON-Operationen
Parse JSON
const data = JSON.parse('{"id": 42}');
import json
data = json.loads('{"id": 42}')Stringify JSON
const json = JSON.stringify({ id: 42, name: 'Alice' });
import json
json_str = json.dumps({'id': 42, 'name': 'Alice'})JSON vs XML vs YAML
| Feature | JSON | XML | YAML |
|---|---|---|---|
| Verbosity | Niedrig | Hoch | Niedrigste |
| Comments | Nein | Ja | Ja |
| Schema-Validierung | JSON Schema | XSD | JSON Schema |
| Am besten für | APIs, Config | Document-Data | Configs, k8s |
JSON in REST-APIs
POST /api/users HTTP/1.1
Content-Type: application/json
{ "email": "alice@example.com", "name": "Alice" }JSON Schema
{
"type": "object",
"required": ["email", "name"],
"properties": {
"email": { "type": "string", "format": "email" },
"name": { "type": "string", "minLength": 1 }
}
}JSON Best Practices
- ISO 8601 für Dates.
- Konsistente Naming.
- Gegen Schema validieren.
- Mit gzip / Brotli komprimieren.
- null statt missing Keys.
- Numbers im JS-Safe-Range.
- Kein Binary in JSON.
- Große Arrays streamen.
Häufige JSON-Fallstricke
- Single Quotes.
- Trailing Commas.
- Comments in Standard-JSON.
- Number-Präzision.
- Undefined Dates.
- Inkonsistente Shapes.
- HTML-Escaping in JSON.
- JSON-Injection.
Varianten von JSON
| Variante | Fügt hinzu | Use Case |
|---|---|---|
| JSON5 | Comments, Trailing Commas | Config-Files |
| JSONC | Comments | VS Code Settings |
| NDJSON | Newline-delimited Records | Log-Streaming |
| JSON-LD | Linked-Data-Semantics | SEO Structured Data |
| GeoJSON | Geographic Shapes | Maps, GIS |
| JSON Patch | Diff-Format | API Partial-Updates |
FAQ: JSON
Warum JSON über XML?
Kleiner, simplere Syntax, native JS-Parsing.
Kann JSON Comments haben?
Standard-JSON: nein. JSON5/JSONC: ja.
Wie sende ich Dates in JSON?
ISO 8601 Strings.
Was über große Numbers?
Über JS Safe Integer als String serialisieren.
Ist JSON sicher von Untrusted-Sources zu parsen?
JSON.parse ist sicher; aber Shape via Schema validieren.
Unterschied zwischen JSON und JS-Objects?
JSON ist Text-Format. JS-Objects sind Runtime-Daten.
Wie pretty-printe ich JSON?
JSON.stringify(obj, null, 2); jq ..
JSON-APIs at Scale mit LoadFocus testen
LoadFocus läuft JMeter- und k6-Scripts, die JSON-APIs aus 25+ Regionen treffen. Registrieren bei loadfocus.com/signup.
Verwandte LoadFocus-Tools
Setze dieses Konzept mit LoadFocus in die Praxis um — derselben Plattform, die alles antreibt, was du gerade gelesen hast.