OpenSearch-Dashboards: [BUG] - OpenSearch Dashboard V2.15.0 - JSON.parse: bad escaped character
Describe the bug
After upgrading from 2.13 to 2.15 Discover in Dashboards is barely usable due to the following error.
SyntaxError: Bad escaped character in JSON at position 911476 (line 1 column 911477) at fetch_Fetch.fetchResponse (https://dashboards.observability-opensearch.backend.ppe.cloud/7749/bundles/core/core.entry.js:15:243032) at async interceptResponse (https://dashboards.observability-opensearch.backend.ppe.cloud/7749/bundles/core/core.entry.js:15:237932) at async https://dashboards.observability-opensearch.backend.ppe.cloud/7749/bundles/core/core.entry.js:15:240899
Related component
Search
To Reproduce
Upgrade to version 2.15.0 and it will appear in the discover.
Expected behavior
See the following error in discover
SyntaxError: Bad escaped character in JSON at position 911476 (line 1 column 911477)
Additional Details
Someone already reported this issue in OpenSearch forum https://forum.opensearch.org/t/json-parse-bad-escaped-character/20211
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Reactions: 4
- Comments: 41 (14 by maintainers)
Commits related to this issue
- Replace JSON11-produced hex escape codes with unicode escape codes JSON11.stringify produces JSON5 documents with hex escape codes (`\x1b`), which aren't standard JSON and cause `JSON.parse` to error.... — committed to wjordan/OpenSearch-Dashboards by wjordan 2 years ago
- Replace JSON11-produced hex escape codes with unicode escape codes JSON11.stringify produces JSON5 documents with hex escape codes (`\x1b`), which aren't standard JSON and cause `JSON.parse` to error.... — committed to wjordan/opensearch-js by wjordan 2 years ago
- Replace JSON11-produced hex escape codes with unicode escape codes JSON11.stringify produces JSON5 documents with hex escape codes (`\x1b`), which aren't standard JSON and cause `JSON.parse` to error.... — committed to wjordan/opensearch-js by wjordan 2 years ago
- Replace JSON11-produced hex escape codes with unicode escape codes JSON11.stringify produces JSON5 documents with hex escape codes (`\x1b`), which aren't standard JSON and cause `JSON.parse` to error.... — committed to wjordan/OpenSearch-Dashboards by wjordan 2 years ago
We have the same issue. Any chance of fixing that in 2.17?
I believe #6915 introduced this bug. Specifically, the new dependency on the
json11node module and the call toJSON11.stringifyproduces JSON5 documents that are not entirely compatible withJSON.parseused elsewhere in this project.Here’s a minimal demo of the underlying issue, using a “hello world” message with the second word wrapped in an ANSI escape code marking the color green.
Using standard
JSON.stringifyround-trips the original text without issue:However, using
JSON11.stringifyconverts the ASCII escape character to\x1b(a valid JSON5 hexadecimal escape, but not valid JSON which only supports\u001bunicode escape), producing an error inJSON.parse:@JannikBrand I also have been unsucessful in indexing a sample of our problematic data manually. Maybe the behaviour of the JSON Parser changes with bigger amounts of data? We are using vector in version 0.39.0 to index our log data. Vector is indexing about 10000 documents at a time using the bulk API with the create action. We have been able to work around this issue using the strip_ansi_escape_codes function of Vector (at least for new logs).
I can confirm that bumping json11 to 2.0.0 fixes the issue. I made a rebuild of 2.16.0, the error is gone.
Hi @agoerl , @JannikBrand and @Christian93. Adding a bit more details about our testing of this above bug. We tried the indexing the document that you have provided us across 2.11, 2.13 & 2.15 versions of Opensearch releases using OSD’s dev tools as well as directly hitting the Opensearch endpoints.
However we were unable to index the document. Adding the API calls that we have tried for reference below.
curl --location --request PUT 'http://localhost:9200/sample-index-2.11/_doc/1' \ --header 'Content-Type: application/json' \ --data '{ "msg": "\x1b[0mPOST /oauth/authorize \x1b[32m200\x1b[0m 95.542 ms - 1499\x1b[0m" }'Response:
{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"json_parse_exception","reason":"Unrecognized character escape 'x' (code 120)\n at [Source: (byte[])\"{\n \"msg\": \"\\x1b[0mPOST /oauth/authorize \\x1b[32m200\\x1b[0m 95.542 ms - 1499\\x1b[0m\"\n}\"; line: 2, column: 15]"}},"status":400}Bulk APi Call:
curl --location 'http://localhost:9200/_bulk' \ --header 'Content-Type: application/json' \ --data '{"index":{"_index":"test_index","_id":"1"}} {"msg": "\x1b[0mPOST /oauth/authorize \x1b[32m200\x1b[0m 95.542 ms - 1499\x1b[0m"} {"index":{"_index":"test_index","_id":"2"}} {"msg": "This is a normal message without escape sequences"} 'Response:
{"took":126,"errors":true,"items":[{"index":{"_index":"test_index","_id":"1","status":400,"error":{"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"json_parse_exception","reason":"Unrecognized character escape 'x' (code 120)\n at [Source: (byte[])\"{\"msg\": \"\\x1b[0mPOST /oauth/authorize \\x1b[32m200\\x1b[0m 95.542 ms - 1499\\x1b[0m\"}\"; line: 1, column: 12]"}}}},{"index":{"_index":"test_index","_id":"2","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1,"status":201}}]}Overall we were unable to index the document you had provided. To investigate this issue we will need your help to understand how you had indexed this document in 2.13 version or older versions and it would also be great if you can share the index mapping for the index in 2.13 version. You can use the following link https://opensearch.org/docs/latest/field-types/ to get the command for obtaining the index mapping.
Regards, cc. @ashwin-pc @ananzh
Issue
Analysis
What we know now:
The error is not silently caught, as we see it on the browser screen.
It’s not from the innermost try-catch block that falls back to
body = text; when JSON parsing fails.The error message mentions fetch_Fetch.fetchResponse, which indicates it’s happening within this
fetchResponsemethod.Given these points, the error is likely coming from one of these two lines:
Reproduce
Given the limited information about the error related
escaped character, the image of discover and location the catch the error, I tried to reproduce with a script which can create 500 documents with 11 fixed fields (10 specific fields plus the timestamp). The content of each field is generated using the functions that create various types of escape sequences, nested objects, arrays, number formats, etc:generate.py
Then index it to 2.15 OS
Create index pattern using a plain 2.15 OSD. Then open in discover and all look good:
Fix / Discussion
Since I can’t reproduce it, there are two ways to proceed:
to