dingo4dev commented on code in PR #62882: URL: https://github.com/apache/airflow/pull/62882#discussion_r2908847017
########## airflow-core/src/airflow/ui/src/utils/json.ts: ########## @@ -0,0 +1,72 @@ +/*! + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +/** + * Checks if a string is valid JSON + * @param string - The string to validate + * @returns true if the string is valid JSON, false otherwise + */ +export const isJsonString = (string: string): boolean => { + try { + JSON.parse(string); + } catch { + return false; + } + + return true; +}; + +/** + * Prettifies a JSON string with indentation + * @param jsonString - The JSON string to prettify + * @param indent - Number of spaces for indentation (default: 2) + * @returns Prettified JSON string, or the original string if parsing fails + */ +export const prettifyJson = (jsonString: string, indent: number = 2): string => { + if (!isJsonString(jsonString)) { + return jsonString; + } + + try { + const parsed: unknown = JSON.parse(jsonString); + + return JSON.stringify(parsed, undefined, indent); Review Comment: the `isJsonString` is return the `true` or `false` value to validate it is a json. After find the jsonString is valid json, need to parse to object and `stringify` again. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
