Technical Guide • 9 min read • Published 2026-10-31

How to Parse and Format JSON across JavaScript, Python, Go, and Java

A practical code handbook for parsing, serializing, and error-handling JSON in modern programming languages: TypeScript/Node.js, Python 3, Golang, and Java 21.

# How to Parse and Format JSON across JavaScript, Python, Go, and Java Parsing and serializing JSON correctly is a fundamental requirement of modern software development. However, every major programming language approaches memory management, type safety, and error handling differently. Below are idiomatic, production-tested implementations across the four most popular backend languages. --- ## 1. JavaScript & TypeScript (Node.js / Browser) JavaScript features built-in global `JSON` methods. In TypeScript, always pair parsing with runtime validation or static typing using our [JSON to TypeScript Generator](/tools/json-to-typescript). ```typescript interface UserProfile { id: number; username: string; email: string; } const rawJson = '{"id": 101, "username": "alex", "email": "alex@example.com"}'; try { // Deserialization (Parse) const user: UserProfile = JSON.parse(rawJson); console.log(`Loaded user: ${user.username}`); // Serialization with 2-space pretty-printing const formatted = JSON.stringify(user, null, 2); console.log(formatted); } catch (error) { console.error("Syntax error parsing JSON:", (error as Error).message); } ``` --- ## 2. Python 3 (Standard Library & Pydantic) Python includes the native `json` module. For modern FastAPI applications, pairing payloads with Pydantic ensures strict type coercion (generate models with our [JSON to Python Pydantic tool](/tools/json-to-python)). ```python import json raw_json = '''{ "id": 101, "name": "Sarah Connor", "is_active": true }''' try: # Deserialization into Python dictionary data = json.loads(raw_json) print(f"User: {data['name']}, Active: {data['is_active']}") # Serialization with sorting and indentation pretty_json = json.dumps(data, indent=4, sort_keys=True) print(pretty_json) except json.JSONDecodeError as exc: print(f"Failed to decode JSON at line {exc.lineno}, col {exc.colno}: {exc.msg}") ``` --- ## 3. Golang (encoding/json) Go requires explicit struct definitions with struct tags. Generate these automatically with our [JSON to Go Struct Generator](/tools/json-to-go). ```go package main import ( "encoding/json" "fmt" "log" ) type User struct { ID int `json:"id"` Username string `json:"username"` Email string `json:"email,omitempty"` } func main() { payload := []byte(`{"id": 101, "username": "gopher", "email": "go@example.com"}`) var user User if err := json.Unmarshal(payload, &user); err != nil { log.Fatalf("Unmarshal error: %v", err) } fmt.Printf("Parsed user: %+v\n", user) // Indented Serialization indented, err := json.MarshalIndent(user, "", " ") if err != nil { log.Fatalf("Marshal error: %v", err) } fmt.Println(string(indented)) } ``` --- ## 4. Java 21 (Jackson ObjectMapper) In enterprise Java, Jackson `ObjectMapper` is the industry standard (generate POJOs with our [JSON to Java POJO Tool](/tools/json-to-java)). ```java import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.core.JsonProcessingException; public class JsonDemo { public static void main(String[] args) { ObjectMapper mapper = new ObjectMapper(); String json = "{"id":101,"name":"James Gosling"}"; try { User user = mapper.readValue(json, User.class); System.out.println("Parsed: " + user.getName()); // Pretty-print JSON String pretty = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(user); System.out.println(pretty); } catch (JsonProcessingException e) { System.err.println("Jackson processing error: " + e.getMessage()); } } } ```
🛠️

Interactive Tools Mentioned in this Guide

Test your JSON data and execute the techniques discussed above with our free in-browser developer utilities: