Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

WRK-871 Details view: JSON formatting missing in details view (#63892)

Kamil Mielnik581c1e7a386b
diff --git a/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.module.css b/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.module.css
index b84536daadf..a5603aced6b 100644
--- a/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.module.css
+++ b/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.module.css
@@ -32,3 +32,11 @@
max-height: 560px;
overflow: auto;
}
+
+.json {
+ max-height: 200px;
+ width: 100%;
+ background-color: var(--mb-color-bg-light);
+ border: 1px solid var(--mb-color-border);
+ border-radius: 4px;
+}
diff --git a/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.tsx b/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.tsx
index b68557c0888..26dbb628e3c 100644
--- a/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.tsx
+++ b/frontend/src/metabase/detail-view/components/DetailsGroup/Value/Value.tsx
@@ -1,10 +1,12 @@
import cx from "classnames";
-import type { ReactNode } from "react";
+import { type ReactNode, useMemo } from "react";
import { Link } from "react-router";
import { t } from "ttag";
+import { CodeEditor } from "metabase/common/components/CodeEditor";
import { Box, Image, Stack, Text, rem } from "metabase/ui";
-import { isFK } from "metabase-lib/v1/types/utils/isa";
+import { TYPE } from "metabase-lib/v1/types/constants";
+import { isFK, isa } from "metabase-lib/v1/types/utils/isa";
import type { DatasetColumn, Field, RowValue } from "metabase-types/api";
import S from "./Value.module.css";
@@ -27,11 +29,23 @@ export const Value = ({ children, column, field, value }: Props) => {
column.settings.link_text &&
column.settings.link_url,
);
+ const json = useMemo(() => getJson(column, value), [column, value]);
if (isEmptyValue) {
return <Text c="text-light">{t`empty`}</Text>;
}
+ if (json) {
+ return (
+ <CodeEditor
+ className={S.json}
+ language="json"
+ lineNumbers={false}
+ value={json}
+ />
+ );
+ }
+
if (isFK(column) && newTableId != null && !isValidLink) {
return (
<Text
@@ -83,3 +97,34 @@ export const Value = ({ children, column, field, value }: Props) => {
</Text>
);
};
+
+function getJson(
+ column: DatasetColumn,
+ value: RowValue | undefined,
+): string | undefined {
+ if (!value || typeof value === "number" || typeof value === "boolean") {
+ return undefined;
+ }
+
+ if (
+ column.semantic_type &&
+ isa(column.semantic_type, TYPE.SerializedJSON) &&
+ typeof value == "string"
+ ) {
+ try {
+ return JSON.stringify(JSON.parse(value), null, 2);
+ } catch (error) {
+ return undefined;
+ }
+ }
+
+ if (typeof value === "object") {
+ try {
+ return JSON.stringify(value, null, 2);
+ } catch (error) {
+ return undefined;
+ }
+ }
+
+ return undefined;
+}