json_encode
json_encode(mixed $value, int $flags = 0, int $depth = 512): string|false
Serializes a value into a JSON string.
C C23
#include <stdio.h>
#include <stdlib.h>
#include "cJSON.h"
int main(void) {
cJSON *root = cJSON_CreateObject();
cJSON_AddStringToObject(root, "name", "Alice");
cJSON_AddNumberToObject(root, "age", 30);
char *out = cJSON_PrintUnformatted(root); // compact output, equivalent to json_encode
printf("%s", out);
cJSON_free(out); // use the paired deallocator provided by cJSON
cJSON_Delete(root);
return 0;
}
{"name":"Alice","age":30}
C++ C++26
#include <iostream>
#include "nlohmann/json.hpp"
int main() {
// nlohmann json sorts keys by default; ordered_json keeps insertion order, matching PHP's output
nlohmann::ordered_json j;
j["name"] = "Alice";
j["age"] = 30;
std::cout << j.dump(); // dump() emits compact JSON, equivalent to json_encode
return 0;
}
{"name":"Alice","age":30}
C# v11.0.100
var obj = new Dictionary<string, object> { ["name"] = "Alice", ["age"] = 30 };
Console.WriteLine(System.Text.Json.JsonSerializer.Serialize(obj));
{"name":"Alice","age":30}
Go v1.27.4
package main
import (
"encoding/json"
"fmt"
)
type Person struct {
Name string `json:"name"`
Age int `json:"age"`
}
func main() {
b, _ := json.Marshal(Person{Name: "Alice", Age: 30})
fmt.Println(string(b))
}
{"name":"Alice","age":30}
Java v27.0.1
import java.io.StringWriter;
import org.json.JSONWriter;
class Main {
public static void main(String[] args) {
// org.json's JSONObject is HashMap-based; key order is not guaranteed;
// serialize in write order with JSONWriter; compact output matches PHP
StringWriter sw = new StringWriter();
JSONWriter w = new JSONWriter(sw);
w.object().key("name").value("Alice").key("age").value(30).endObject();
System.out.print(sw.toString());
}
}
{"name":"Alice","age":30}
JavaScript v26.13.1
console.log(JSON.stringify({ name: "Alice", age: 30 }))
{"name":"Alice","age":30}
PHP v8.6.0
echo json_encode(["name" => "Alice", "age" => 30]);
{"name":"Alice","age":30}
Python v3.15.2
import json
print(json.dumps({"name": "Alice", "age": 30}))
{"name": "Alice", "age": 30}
Rust v1.99.1
fn main() {
// serde_json's json! macro builds JSON; Cargo.toml enables preserve_order to keep insertion order
let j = serde_json::json!({"name": "Alice", "age": 30});
println!("{}", j.to_string());
}
{"name":"Alice","age":30}
TypeScript v26.13.1
const obj: Record<string, string | number> = { name: "Alice", age: 30 };
console.log(JSON.stringify(obj));
{"name":"Alice","age":30}
Language differences
C: requires a third-party library for JSON — the example uses the vendored cJSON (MIT), compiled alongside the snippet.
C++: the standard library has no JSON — the example uses the vendored nlohmann/json single header (ordered_json preserves insertion order, matching PHP output).
C#: serializes with System.Text.Json.JsonSerializer.Serialize; the Dictionary enumerates in insertion order in current .NET, so serialization keeps key order, but the API contract does not guarantee that order.
Go: uses struct tags to control field names.
Java: the standard library has no JSON support and needs a third-party library — the example uses the vendored org.json (JSONWriter serializes in write order; JSONObject is HashMap-based with no key-order guarantee).
JavaScript: compact output (like PHP).
PHP: compact output.
Python: adds spaces after : and , by default (disable with separators).
Rust: needs serde_json — the json! macro builds the value and to_string serializes it (preserve_order enabled to keep insertion order, matching PHP output).