JSON in Other Languages
JSON is language-independent. Every mainstream language can parse and generate it, and the workflow is always the same two steps you already know from JavaScript: parse text into native data structures, and serialize data structures back into text. What changes between languages is mainly how typed the result is.
Two styles of JSON handling
| Style | How it works | Pros | Cons |
|---|---|---|---|
| Dynamic / tree | Parse into generic maps, lists and values | Works with any shape | You check types and keys by hand |
| Typed / binding | Parse directly into your own classes or structs | Compiler checks field names and types | You must define the types first |
JavaScript and Python default to the dynamic style. Java, C#, Go and Rust usually use the typed style.
In all the examples below we use the same document:
{"name": "Desk Lamp", "price": 18.5, "tags": ["light", "office"], "inStock": true}Valid JSON: object with 4 keys
{
"name": "Desk Lamp",
"price": 18.5,
"tags": [
"light",
"office"
],
"inStock": true
}
Java (Jackson)
Java has no JSON parser in its standard library; Jackson and Gson are the most popular libraries. With Jackson you define a class (or record) and let ObjectMapper fill it:
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;
record Product(String name, double price, List<String> tags, boolean inStock) {}
public class Demo {
public static void main(String[] args) throws Exception {
ObjectMapper mapper = new ObjectMapper();
String json = "{\"name\":\"Desk Lamp\",\"price\":18.5,\"tags\":[\"light\",\"office\"],\"inStock\":true}";
Product p = mapper.readValue(json, Product.class); // parse
System.out.println(p.name() + " costs " + p.price());
String out = mapper.writeValueAsString(p); // serialize
System.out.println(out);
}
}C# (System.Text.Json)
.NET includes System.Text.Json. JsonSerializer maps JSON to classes; options handle naming differences such as inStock vs InStock:
using System.Text.Json;
var options = new JsonSerializerOptions { PropertyNameCaseInsensitive = true };
string json = """{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}""";
Product? p = JsonSerializer.Deserialize<Product>(json, options);
Console.WriteLine(quot;{p!.Name} costs {p.Price}");
string output = JsonSerializer.Serialize(p, new JsonSerializerOptions { WriteIndented = true });
Console.WriteLine(output);
record Product(string Name, double Price, List<string> Tags, bool InStock);Go (encoding/json)
Go's standard library uses struct tags to map JSON keys to struct fields. Only exported (capitalised) fields are serialized:
package main
import (
"encoding/json"
"fmt"
)
type Product struct {
Name string `json:"name"`
Price float64 `json:"price"`
Tags []string `json:"tags"`
InStock bool `json:"inStock"`
}
func main() {
data := []byte(`{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}`)
var p Product
if err := json.Unmarshal(data, &p); err != nil {
panic(err)
}
fmt.Println(p.Name, "costs", p.Price)
out, _ := json.Marshal(p)
fmt.Println(string(out))
}Rust (serde_json)
Rust's ecosystem standard is serde. Deriving Serialize and Deserialize generates all the conversion code at compile time:
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
struct Product {
name: String,
price: f64,
tags: Vec<String>,
in_stock: bool,
}
fn main() -> Result<(), serde_json::Error> {
let text = r#"{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}"#;
let p: Product = serde_json::from_str(text)?;
println!("{} costs {}", p.name, p.price);
println!("{}", serde_json::to_string_pretty(&p)?);
Ok(())
}Notice rename_all = "camelCase": the Rust field in_stock maps to the JSON key inStock.
The same idea in JavaScript
For comparison, the dynamic version — no type definitions, but also no checks:
const text = '{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}';
const p = JSON.parse(text);
console.log(`${p.name} costs ${p.price}`);
console.log(p.colour); // typo-like access: no error, just undefined
console.log(JSON.stringify(p));Desk Lamp costs 18.5
undefined
{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}
And in Python
import json
text = '{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}'
p = json.loads(text)
print(f"{p['name']} costs {p['price']}")
print(", ".join(p["tags"]))
print(json.dumps(p, separators=(",", ":")))Desk Lamp costs 18.5
light, office
{"name":"Desk Lamp","price":18.5,"tags":["light","office"],"inStock":true}
Quick reference
| Language | Library | Parse | Serialize |
|---|---|---|---|
| JavaScript | built in | JSON.parse(s) |
JSON.stringify(v) |
| Python | json (std lib) |
json.loads(s) |
json.dumps(v) |
| Java | Jackson | mapper.readValue(s, T.class) |
mapper.writeValueAsString(v) |
| C# | System.Text.Json |
JsonSerializer.Deserialize<T>(s) |
JsonSerializer.Serialize(v) |
| Go | encoding/json |
json.Unmarshal(b, &v) |
json.Marshal(v) |
| Rust | serde_json |
serde_json::from_str(s) |
serde_json::to_string(&v) |
| PHP | built in | json_decode($s, true) |
json_encode($v) |
Naming conventions across languages
JSON keys are often camelCase, while Python and Rust prefer snake_case and C# prefers PascalCase. Every library has a way to map between them — Jackson's @JsonProperty, C#'s JsonPropertyName or naming policies, Go's struct tags and serde's rename. Pick one style for your JSON and let each language's mapping handle the rest.
Tip: Typed binding catches problems early: a missing required field or a string where a number belongs causes a clear error at the boundary instead of a confusing bug later.
Test yourself
In Go, what tells encoding/json which JSON key belongs to a struct field?
- A comment
- A struct tag like
json:"name" - The field order
- An annotation @Json
Which Rust crate is the usual choice for JSON?
- tokio
- serde_json
- rand
- regex
What is an advantage of typed (binding) JSON handling?
- It works without knowing the shape
- Wrong field names and types are caught early
- It produces smaller JSON
- It allows comments