全部展開/全部摺疊 IsJson - 指令碼與圖表函數在此頁面
IsJson() 測試指定的字串是否包含有效的 JSON (JavaScript 物件標記法) 資料。您也可以驗證特定的 JSON 資料類型。
語法:
value IsJson(json [, type])
傳回的資料類型: 雙值
引數
json
要測試的字串。這可以包含額外的空間或新行。
type
指定要測試之 JSON 資料類型的選用引數。
'值' (預設)
'物件'
'陣列'
'字串'
'數字'
'布林'
'null'
範例:有效和無效的圖表運算式
IsJson( 'null' )
傳回 -1 (true)
IsJson( '"abc"', 'value' )
傳回 -1 (true)
IsJson( '"abc"', 'string' )
傳回 -1 (true)
IsJson( 123, 'number' )
傳回 -1 (true)
IsJson( 'text' )
傳回 0 (false) ,'text' 不是有效的 JSON 值
IsJson( '"text"', 'number' )
傳回 0 (false) ,'"text"' 不是有效的 JSON 數字
IsJson( '"text"', 'text' )
傳回 0 (false) ,'text' 不是有效的 JSON 類型
範例 - IsJson 基礎事項 圖表運算式 概述
開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。
載入指令碼包含:
載入指令碼
Example:
Load
Recno() AS ID, API_Response
inline [
API_Response
'{"id": 1, "name": "Alice"}'
'{"id": 2, "name": "Bob"}'
'{invalid json string}'
'{"id": 4, "name": "Charlie"}'
'{"id": 5, name: "David"}'
];
將代碼複製到剪貼簿 結果
載入資料並開啟工作表。建立新的表格並將這些欄位新增為維度:
建立下列計算維度:
結果表格 ID API_Response IsJson(API_Response) 1 {"id": 1, "name": "Alice"} -1 (true) 2 {"id": 2, "name": "Bob"} -1 (true) 3 {invalid json string} 0 (false) 4 {"id": 4, "name": "Charlie"} -1 (true) 5 {"id": 5, name: "David"} 0 (false)
對於具有有效 JSON 語法的值,輸出會傳回 -1 或 true 。
對於以下具有無效 JSON 的 ID 記錄 (3 和 5 ),輸出會傳回 0 或 false :
範例 - IsJson 使用情境 圖表運算式 概述
開啟資料載入編輯器並將下面的載入指令碼新增至新的區段。
載入指令碼包含:
載入指令碼
Example:
Load *
INLINE [OrderDetails
'{ "order_id": "12345", "customer": { "name": "John Doe", "email": "john.doe@example.com"}, "items": {"product": "Laptop", "quantity": 2, "price": 1200 }, "total_price": 2400 }'
];
將代碼複製到剪貼簿 結果
載入資料並開啟工作表。建立新的表格並將此欄位新增為維度:
建立下列量值:
IsJson( OrderDetails) ,用來計算 OrderDetails 中的值是否為有效的 JSON。
IsJson( JsonGet ( OrderDetails, '/items/price' ), 'number' ) ,使用函數 JsonGet ,可擷取 price 索引鍵的 JSON 文字並驗證 price 的值是否為數字。
結果表格 OrderDetails IsJson(OrderDetails) IsJson( JsonGet ( OrderDetails, '/items/price' ), 'number' ) { "order_id": "12345", "customer": { "name": "John Doe", "email": "john.doe@example.com"}, "items": {"product": "Laptop", "quantity": 2, "price": 1200 }, "total_price": 2400 } -1 (true) -1 (true)
第一個量值傳回 -1 (true) ,因為 OrderDetails 包含有效的 JSON 語法。
第二個量值傳回 -1 (true) ,因為 price 索引鍵的值是有效數字 1200 。
另請參見: