json_last_error_msg
json_last_error_msg(): string
Turns the error code of the most recent JSON operation into a human-readable English message; pairs with json_last_error.
C C23
#include <stdio.h>
#include "cJSON.h"
int main(void) {
cJSON *root = cJSON_Parse("{bad");
if (root == NULL) {
/* cJSON has no message string - only the error byte position */
const char *err = cJSON_GetErrorPtr();
printf("parse failed: %s\n", err != NULL ? "position known" : "no position");
return 0;
}
printf("ok\n");
cJSON_Delete(root);
return 0;
}
parse failed: position known
C++ C++26
#include <iostream>
#include <string>
#include "nlohmann/json.hpp"
int main() {
try {
nlohmann::json::parse("{bad");
std::cout << "ok";
} catch (const nlohmann::json::parse_error& e) {
// e.what() carries the human-readable message + byte position
std::cout << "has message: " << (std::string(e.what()).size() > 0 ? "true" : "false");
}
return 0;
}
has message: true
C# v11.0.100
try {
System.Text.Json.JsonDocument.Parse("{bad");
Console.WriteLine("ok");
} catch (System.Text.Json.JsonException e) {
// JsonException.Message carries position detail like PHP 8.6 wording
Console.WriteLine("has message: " + (e.Message.Length > 0));
}
has message: True
Go v1.27.4
package main
import (
"encoding/json"
"errors"
"fmt"
)
func main() {
var v interface{}
err := json.Unmarshal([]byte("{bad"), &v)
fmt.Println("has error:", err != nil)
// errors.As with *json.SyntaxError is the typed counterpart of PHP's
// JSON_ERROR_SYNTAX (code 4)
var synErr *json.SyntaxError
fmt.Println("is syntax error:", errors.As(err, &synErr))
}
has error: true is syntax error: true
Java v27.0.1
import org.json.JSONObject;
class Main {
public static void main(String[] args) {
try {
new JSONObject("{bad");
System.out.println("ok");
} catch (org.json.JSONException e) {
// getMessage() is the human-readable counterpart of json_last_error_msg()
String msg = e.getMessage();
System.out.println("has message: " + (msg != null && !msg.isEmpty()));
}
}
}
has message: true
JavaScript v26.13.1
try {
JSON.parse("{bad");
console.log("ok");
} catch (e) {
console.log(e.name); // SyntaxError - the stable identity
console.log("has message:", e.message.length > 0 ? true : false);
}
SyntaxError has message: true
PHP v8.6.0
// Before any encode/decode the global error state is "no error"
var_dump(json_last_error_msg());
json_decode("{bad");
echo "code: ", json_last_error(), "\n"; // 4 = JSON_ERROR_SYNTAX (stable)
// The message wording drifts across versions (8.6 adds location detail);
// assert the stable family prefix instead of the full sentence
var_dump(json_last_error_msg());
echo "prefix ok: ", str_starts_with(json_last_error_msg(), "Syntax error") ? "yes" : "no";
string(8) "No error" code: 4 string(30) "Syntax error near location 1:2" prefix ok: yes
Python v3.15.2
import json
try:
json.loads("{bad")
print("ok")
except json.JSONDecodeError as e:
print(type(e).__name__) # stable identity (like PHP's error code)
print("has message:", len(str(e)) > 0) # str(e) carries line/column detail
JSONDecodeError has message: True
Rust v1.99.1
// serde_json: parse failures come back as Err; classify() is the stable
// category (PHP's error-code analogue), Display is the message
use serde_json::error::Category;
fn main() {
let result = serde_json::from_str::<serde_json::Value>("{bad");
match result {
Ok(_) => println!("ok"),
Err(e) => {
println!("category syntax: {}", e.classify() == Category::Syntax);
println!("has message: {}", !e.to_string().is_empty());
}
}
}
category syntax: true has message: true
TypeScript v26.13.1
try {
JSON.parse("{bad");
console.log("ok");
} catch (e) {
const err = e as Error; // TS: catch variable is unknown, narrow first
console.log(err.name);
console.log("has message:", err.message.length > 0 ? true : false);
}
SyntaxError has message: true
Language differences
C: the C standard library has no JSON — the vendor cJSON exposes only the error byte position via cJSON_GetErrorPtr, no message string (as written in the example).
C++: nlohmann's parse_error carries a human-readable what() message plus byte position; its id field corresponds to an error code.
C#: JsonException.Message includes position detail (line number / byte offset), in the same spirit as PHP 8.6 wording.
Go: encoding/json models errors as a type hierarchy — *json.SyntaxError and friends correspond to PHP's error-code constants; errors.As extracts them and exposes Offset.
Java: org.json throws JSONException whose getMessage() is the human-readable message (example); Jackson offers finer JsonLocation data.
JavaScript: JSON.parse throws a SyntaxError — name is the stable identity, message wording drifts by engine version (assert only the name across versions).
PHP: error querying is "global state + two levels" — json_last_error returns the stable code, json_last_error_msg returns the wording; wording drifts by version (8.6+ adds position to syntax errors), so cross-version assertions should rely on the code or a prefix (as written); with no operation yet it returns No error.
Python: json.JSONDecodeError has a short msg attribute and str(e) with line/column position; position attributes (lineno/colno) are programmatically consumable.
Rust: serde_json errors come in five stable Category values (io/syntax/data/eof/other), the semantic tiers of PHP's error codes; to_string() is the message.
TypeScript: runtime identical to JavaScript; the catch variable defaults to unknown, narrow before reading message (example).