json_last_error

json_last_error(): int

Returns the error code of the most recent JSON encode/decode operation.

C C23

C23
#include <stdio.h>
#include "cJSON.h"

int main(void) {
    cJSON *root = cJSON_Parse("{bad");
    if (root == NULL) {
        // parsing failure returns NULL (error position available via cJSON_GetErrorPtr);
        // here we print error code 4 (JSON_ERROR_SYNTAX) and the message, aligned with PHP
        printf("4: Syntax error");
        return 0;
    }
    printf("No error");
    cJSON_Delete(root);
    return 0;
}
Output
4: Syntax error

C++ C++26

C++26
#include <iostream>
#include "nlohmann/json.hpp"

int main() {
    try {
        nlohmann::json::parse("{bad"); // parsing failure throws parse_error
        std::cout << "No error";
    } catch (const nlohmann::json::parse_error &) {
        // exceptions correspond to PHP querying a global error state; here we align with the error code 4 output
        std::cout << "4: Syntax error";
    }
    return 0;
}
Output
4: Syntax error

C# v11.0.100

v11.0.100
try {
    System.Text.Json.JsonDocument.Parse("{bad");
    Console.WriteLine("ok");
} catch (System.Text.Json.JsonException e) {
    // PHP queries a global error state after each json operation; C# signals parse failures via exceptions
    Console.WriteLine($"JsonException: {e.Message}");
}
Output
JsonException: 'b' is an invalid start of a property name. Expected a '"'. LineNumber: 0 | BytePositionInLine: 1.

Go v1.27.4

v1.27.4
package main

import (
    "encoding/json"
    "fmt"
)

func main() {
    var v interface{}
    err := json.Unmarshal([]byte("{bad"), &v)
    fmt.Println(err != nil)
}
Output
true

Java v27.0.1

v27.0.1
import org.json.JSONObject;

class Main {
    public static void main(String[] args) {
        try {
            new JSONObject("{bad"); // parsing failure throws JSONException
            System.out.print("No error");
        } catch (org.json.JSONException e) {
            // exceptions correspond to PHP querying a global error state; here we align with the error code 4 output
            System.out.print("4: Syntax error");
        }
    }
}
Output
4: Syntax error

JavaScript v26.13.1

v26.13.1
try {
    JSON.parse("{bad");
    console.log("ok");
} catch (e) {
    console.log(e.name);
}
Output
SyntaxError

PHP v8.6.0

v8.6.0
json_decode("{bad");
echo "code: ", json_last_error(), "
"; // 4 = JSON_ERROR_SYNTAX (stable across versions)
// The msg wording drifts across versions ("Syntax error near location 1:2" since PHP 8.6); assert only the stable substring
echo "syntax error: ", str_contains(json_last_error_msg(), "Syntax error") ? "yes" : "no", "
";
Output
code: 4
syntax error: yes

Python v3.15.2

v3.15.2
import json

try:
    json.loads("{bad")
    print("ok")
except json.JSONDecodeError:
    print("error")
Output
error

Rust v1.99.1

v1.99.1
fn main() {
    // serde_json returns the parse result as a Result (vs PHP explicitly querying json_last_error)
    match serde_json::from_str::<serde_json::Value>("{bad") {
        Ok(_) => println!("No error"),
        // Category::Syntax corresponds to PHP error code 4 (JSON_ERROR_SYNTAX)
        Err(e) if e.classify() == serde_json::error::Category::Syntax => println!("4: Syntax error"),
        Err(_) => println!("Other error"),
    }
}
Output
4: Syntax error

TypeScript v26.13.1

v26.13.1
try {
    JSON.parse("{bad");
    console.log("ok");
} catch (e) {
    console.log((e as Error).name); // TS: the catch variable defaults to unknown and needs narrowing
}
Output
SyntaxError

Language differences

C: cJSON returns NULL on parse failure (error location via cJSON_GetErrorPtr); the example prints PHP error code 4 for parity.

C++: nlohmann throws parse_error on invalid input (PHP queries a global error state instead); the example prints PHP error code 4 for parity.

C#: raises JsonException on parse failure (PHP queries a global error state instead).

Go: returns an error from the function.

Java: org.json throws JSONException on invalid input (PHP queries a global error state instead); the example prints PHP error code 4 for parity.

JavaScript: raises a SyntaxError exception.

PHP: requires an explicit check of json_last_error (error code 4 is a syntax error); json_last_error_msg() wording drifts by version (8.6+ adds location details) — assert only the code across versions.

Python: raises a json.JSONDecodeError exception.

Rust: uses serde_json error classification (classify yields Category::Syntax, PHP code 4); no global error state to query.