Skip to content

Commit 20546ce

Browse files
fix(json-formatter): collapsable json path output
1 parent 564eb18 commit 20546ce

1 file changed

Lines changed: 15 additions & 7 deletions

File tree

client/src/components/tools/json-formatter.tsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,13 @@ export default function JSONFormatter() {
1717
indentSize: "2",
1818
jsonPath: "",
1919
pathResult: "",
20+
pathResultParsed: null as any,
2021
parsedJson: null as any,
2122
collapsedNodes: [] as string[],
2223
displayMode: "formatted" as "formatted" | "minified"
2324
});
2425

25-
const { input, output, isValid, indentSize, jsonPath, pathResult, parsedJson, displayMode } = state;
26+
const { input, output, isValid, indentSize, jsonPath, pathResult, pathResultParsed, parsedJson, displayMode } = state;
2627

2728
const updateState = (updates: Partial<typeof state>) => {
2829
setState({ ...state, ...updates });
@@ -85,24 +86,30 @@ export default function JSONFormatter() {
8586

8687
const searchJsonPath = () => {
8788
if (!parsedJson) {
88-
updateState({ pathResult: "Please format or validate JSON first" });
89+
updateState({ pathResult: "Please format or validate JSON first", pathResultParsed: null });
8990
return;
9091
}
9192

9293
if (!jsonPath.trim()) {
93-
updateState({ pathResult: "" });
94+
updateState({ pathResult: "", pathResultParsed: null });
9495
return;
9596
}
9697

9798
try {
9899
const result = JSONPath({ path: jsonPath, json: parsedJson });
99100
if (result.length === 0) {
100-
updateState({ pathResult: "No matches found" });
101+
updateState({ pathResult: "No matches found", pathResultParsed: null });
101102
} else {
102-
updateState({ pathResult: JSON.stringify(result, null, 2) });
103+
updateState({
104+
pathResult: JSON.stringify(result, null, 2),
105+
pathResultParsed: result
106+
});
103107
}
104108
} catch (error) {
105-
updateState({ pathResult: `JSONPath Error: ${error instanceof Error ? error.message : 'Invalid path'}` });
109+
updateState({
110+
pathResult: `JSONPath Error: ${error instanceof Error ? error.message : 'Invalid path'}`,
111+
pathResultParsed: null
112+
});
106113
}
107114
};
108115

@@ -143,6 +150,7 @@ export default function JSONFormatter() {
143150
isValid: null,
144151
jsonPath: "",
145152
pathResult: "",
153+
pathResultParsed: null,
146154
parsedJson: null,
147155
displayMode: "formatted"
148156
});
@@ -340,7 +348,7 @@ export default function JSONFormatter() {
340348
<div
341349
className="text-foreground font-mono text-sm whitespace-pre-wrap"
342350
dangerouslySetInnerHTML={{
343-
__html: highlightJSON(pathResult)
351+
__html: pathResultParsed ? renderCollapsibleJSON(pathResultParsed, "pathResult") : highlightJSON(pathResult)
344352
}}
345353
/>
346354
) : (

0 commit comments

Comments
 (0)