SoFunction
Updated on 2025-04-11

Detailed explanation of how to quickly locate and resolve JSON errors (taking Protobuf as an example)

1. Problem background

During a daily development, the following error message appears in our system log:

2025-03-03 18:02:29.840 | ERROR | http-nio-8066-exec-1 |  | Failed to send a channel ad request: 
$ParseException: 1:362: Expected string.
	at $(:765)
	at $(:715)
	at (:1059)
	...

As can be seen from the error log, the problem lies in, the specific reason is that when the program parses JSON data, it expects to get a string, but the actual data does not meet expectations. Next, we will analyze in detail how to locate and solve this problem.

2. Problem analysis

1. Interpretation of error logs

First, we need to extract the key information from the error log:

  • Error Type$ParseException
  • error messageExpected string.
  • Error location: At the 362nd character of line 1
  • Call stack: The error occurred inLine 279 of the method

Through this information, we can initially judge that the problem is caused by incorrect JSON data format.

2. Possible Causes

  • JSON data format error: The value of a certain field should be a string, but it is actually other types (such as numbers, boolean values, etc.).
  • Data source issues: If the JSON data is obtained from an external interface, it may be that the data returned by the interface is incorrect.
  • Code logic issues: The code logic may be flawed when generating or parsing JSON data.

3. Problem positioning

1. View the call stack

As you can see from the call stack, the error occurred inLine 279 of the method. We need to look at the code of this method and find the part that parses the JSON data.

Assume the code is as follows:

public void getAdvertising() {
    String jsonData = getJsonDataFromSomewhere(); // Get JSON data    try {
        (jsonData, builder); // parse JSON data    } catch ( e) {
        ("Failed to parse JSON data", e);
        throw new RuntimeException("Failed to parse JSON data", e);
    }
}

2. Print JSON data

To further analyze the problem, we can print out the JSON data before parsing:

public void getAdvertising() {
    String jsonData = getJsonDataFromSomewhere(); // Get JSON data    ("Received JSON data: {}", jsonData); // Print JSON data    try {
        (jsonData, builder); // parse JSON data    } catch ( e) {
        ("Failed to parse JSON data", e);
        throw new RuntimeException("Failed to parse JSON data", e);
    }
}

Through log output, we can view the actual JSON data and check whether there are format problems.

3. Check the data source

If the JSON data is obtained from an external interface, we need to check whether the returned data of the interface is correct. You can manually request the interface using a tool such as Postman or curl to view the returned JSON data.

For example, use the curl command:

curl -X GET /api/getAdData

If the returned data format is incorrect, you may need to communicate with the interface provider to ensure that the returned data format is in line with expectations.

4. Problem solving

1. Fix JSON data format

Suppose the JSON data we see from the log is as follows:

{
    "adId": 12345,
    "adName": "Test Ad",
    "adTarget": "",
    "adType": 1
}

According to the error message, the program expects to get a string at the 362nd character on line 1. We need to check if there are fields in the JSON data that have incorrect value types.

For example, ifadTypeThe value of the field should be a string, but it is actually a number, we can change it to a string:

{
    "adId": 12345,
    "adName": "Test Ad",
    "adTarget": "",
    "adType": "1"
}

2. Modify the code logic

If the problem is caused by code logic, we need to modify the logic that generates or parses JSON data. For example, make sure that all fields have value types as expected:

public String generateJsonData() {
    JsonObject jsonObject = new JsonObject();
    ("adId", 12345);
    ("adName", "Test Ad");
    ("adTarget", "");
    ("adType", "1"); // Make sure adType is a string    return ();
}

3. Add data verification

To prevent similar problems from happening again, we can add data verification logic before parsing JSON data:

public void getAdvertising() {
    String jsonData = getJsonDataFromSomewhere(); // Get JSON data    ("Received JSON data: {}", jsonData); // Print JSON data
    // Data verification    if (!isValidJson(jsonData)) {
        ("JSON data format is incorrect");
        throw new RuntimeException("JSON data format is incorrect");
    }

    try {
        (jsonData, builder); // parse JSON data    } catch ( e) {
        ("Failed to parse JSON data", e);
        throw new RuntimeException("Failed to parse JSON data", e);
    }
}

private boolean isValidJson(String jsonData) {
    try {
        new JsonParser().parse(jsonData);
        return true;
    } catch (JsonSyntaxException e) {
        return false;
    }
}

5. Summary

Through the above steps, we successfully locate and resolve the JSON parsing error. To sum up, the key steps to solve similar problems include:

  1. Analyze error logs: Extract key information and determine the type and location of the problem.
  2. Print and check data: Check the format of JSON data through log output or tool.
  3. Repair data or code: Fix JSON data format or code logic according to the cause of the problem.
  4. Add data verification: Prevent similar problems from happening again.

In actual development, JSON parsing errors are a common but easily overlooked problem. Through the case analysis and solutions in this article, we hope to help developers better deal with similar problems and improve the stability and reliability of the system.

The above is a detailed explanation of how to quickly locate and resolve JSON errors (taking Protobuf as an example). For more information on locate and resolve JSON errors, please pay attention to my other related articles!