Skip to content

Commit ddd43d3

Browse files
committed
better json objects parsing
1 parent bfafb1a commit ddd43d3

File tree

1 file changed

+11
-12
lines changed

1 file changed

+11
-12
lines changed

application/frontend/src/utils/functions.js

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,22 @@ export const emulateData = (amount) => {
2222
};
2323

2424
export const tryToParseLogString = (str) => {
25-
const beginningOfJson = str.indexOf("{");
26-
const endingOfJson = str.lastIndexOf("}");
25+
const beginningOfJson = str.search(/[{[]/);
26+
const endingOfJson = str.search(/[\]}](?![\s\S]*[\]}])/);
2727

2828
let html = "";
2929
let startText = "";
3030
let endText = "";
3131

32-
if (beginningOfJson !== -1 && endingOfJson !== -1) {
33-
if (endingOfJson > beginningOfJson) {
34-
const jsonPart = str.slice(beginningOfJson, endingOfJson);
35-
startText = str.slice(0, beginningOfJson);
36-
endText = str.slice(endingOfJson + 1, -1);
37-
try {
38-
let normilizedStr = JSON.parse(jsonPart + "}");
39-
html = json2html(normilizedStr, 2);
40-
} catch (e) {}
41-
}
32+
if (beginningOfJson !== -1 && endingOfJson !== -1 && endingOfJson > beginningOfJson) {
33+
const jsonPart = str.slice(beginningOfJson, endingOfJson + 1);
34+
startText = str.slice(0, beginningOfJson);
35+
endText = str.slice(endingOfJson + 1);
36+
37+
try {
38+
const parsed = JSON.parse(jsonPart);
39+
html = json2html(parsed, 2);
40+
} catch (e) { }
4241
}
4342

4443
if (html) {

0 commit comments

Comments
 (0)