Why JSON Parsing Fails After Adding a Field: Java vs JavaScript Insights
During a workflow form update, adding a new JSON attribute caused front‑end errors; the article investigates how JavaScript and Java (fastjson) handle control characters differently, presents experiments, code snippets, and offers practical recommendations to avoid similar JSON parsing issues.
During a workflow‑form release, a new attribute needed to be added to a JSON field stored in historical tickets. The team queried the database via ODC, wrote a script to update the JSON, and generated SQL for production.
String target = JSON.toJSONString(JSON.parseObject(oraData).put("key2","value2"));After deployment the data appeared correct, but the next day some tickets caused front‑end errors and could not be displayed. The issue was traced to the JSON containing a carriage‑return character; removing the line break allowed the page to parse correctly.
Problem analysis
2.1 How JavaScript parses JSON strings
A quick test in Chrome console shows that a string containing an escaped newline is first unescaped by the JavaScript engine and then parsed by JSON.parse, which performs a second unescaping, turning \n into an actual newline character.
var str = "{\"key\":\"v1\
v2\"}";
console.log(str); // {"key":"v1
v2"}
console.log(JSON.parse(str)); // {key: "v1
v2"}
console.log(JSON.parse(str).key); // v1
// v2Thus JavaScript automatically interprets control characters, which can trigger parsing exceptions when the JSON contains raw newline bytes.
2.2 How Java parses JSON strings (fastjson)
Using Alibaba's fastjson library, the same JSON string is parsed without error. The library also performs an initial unescaping of escape sequences, but it tolerates control characters.
Map<String, String> testMap = new HashMap<>();
testMap.put("key", "v1
v2");
System.out.println("1 >> " + JSON.toJSONString(testMap));
String testStr1 = "{\"key\":\"v1
v2\"}";
System.out.println("2 >> " + testStr1);
System.out.println("3 >> " + JSON.parseObject(testStr1).getString("key"));
String testStr2 = "{\"key\":\"v1\
v2\"}";
System.out.println("4 >> " + testStr2);
System.out.println("5 >> " + JSON.parseObject(testStr2).getString("key"));Output demonstrates that fastjson converts the escaped newline to an actual newline and can still retrieve the value.
Reading the source of fastjson 's scanString method confirms that it handles escape sequences (e.g., \n, \t) by converting them to their corresponding characters.
2.3 Interaction between Java and JavaScript causing JSON issues
When JavaScript encodes JSON, it adds an extra escape for control characters, resulting in \\n. Fastjson, however, decodes \n to a newline byte. After the data is processed by a Java script and re‑encoded, the JSON contains a raw newline, which the front‑end then fails to parse.
Original data: [{ "key1": "v1\
v2" }]
Updated data: [{ "key1": "v1
v2" }, { "key2": "value2" }]
Desired data: [{ "key1": "v1\
v2" }, { "key2": "value2" }]To fix the problem, the JSON string should be escaped a second time (i.e., convert \ to \\) before sending it to the front‑end, ensuring that two rounds of unescaping restore the intended characters.
Takeaways
1. Keep data encoding and decoding responsibilities on a single side—preferably the back‑end—to avoid mismatched handling.
2. When upgrading or swapping SDKs for data processing, verify their behavior with edge cases such as control characters, as subtle differences can lead to production incidents.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
