本文介紹JSON和XML結構化數據函數的語法規則,包括參數解釋、函數示例等。
函數列表
類型 | 函數 | 說明 |
JSON | 根據JMES語法提取或計算JSON表達式中特定的值。 | |
將值解析為JSON對象。 | ||
XML | 將xml數據轉成JSON數據。 |
json_select
根據JMES語法提取或計算JSON表達式中特定的值。
函數格式
json_select(value, jmes, default=None, restrict=False)
參數說明
參數
類型
是否必填
說明
value
String、JSON
是
傳入待提取字段的JSON表達式或字段。
jmes
String
是
JMES表達式,表示提取的字段。
default
String
否
如果提取字段不存在,則返回此處設置的值。默認為None,表示不返回字段。
restrict
Bool
否
提取字段的值不是合法的JSON格式時,是否嚴格限制加工。 默認值False。
False:忽略報錯,數據加工繼續處理,返回default定義的值。
True:直接報錯,數據加工不再繼續處理,直接丟棄該條日志。
返回結果
返回提取到的值。
函數示例
示例1:從content字段提取元素name的值。
原始日志
content: {"name": "xiaoming", "age": 10}
加工規則
e_set("json_filter",json_select(v("content"), "name"))
加工結果
content: {"name": "xiaoming", "age": 10} json_filter: xiaoming
示例2:從content字段提取元素name包含的所有值。
原始日志
content: {"name": ["xiaoming", "xiaowang", "xiaoli"], "age": 10}
加工規則
e_set("json_filter", json_select(v("content"), "name[*]"))
加工結果
content: {"name": ["xiaoming", "xiaowang", "xiaoli"], "age": 10} json_filter: ["xiaoming", "xiaowang", "xiaoli"]
示例3:從content字段提取元素name3的值,若字段不存在,則返回default的值。
原始日志
content: {"name": "xiaoming", "age": 10}
加工規則
e_set("json_filter", json_select(v("content"), "name3", default="None"))
加工結果
content: {"name": "xiaoming", "age": 10} json_filter: None
示例4:從content字段提取攜帶短劃線(-)的元素name-test的值。
原始日志
content: {"name": {"name-test":"xiaoming"}, "age": 10}
加工規則
e_set("json_filter", json_select(v("content"), 'name."name-test"', default=None))
加工結果
content: {"name": {"name-test":"xiaoming"}, "age": 10} json_filter: xiaoming
示例5:從content字段提取攜帶短劃線(-)的元素name-test的值,若元素不存在,則不返回字段。
原始日志
content: {"name": {"name.test":"xiaoming"}, "age": 10}
加工規則
e_set("json_filter", json_select(v("content"), 'name."name-test"', default=None))
加工結果
content: {"name": {"name-test":"xiaoming"}, "age": 10}
json_parse
將值解析為JSON對象。
函數格式
json_parse(value, default=None, restrict=False)
參數說明
參數
類型
是否必填
說明
value
String
是
傳入需要被解析的字段。
default
String
否
如果解析字段不存在,則返回此處設置的值。默認為None,表示不返回字段。
restrict
Bool
否
解析字段的值不是合法的JSON格式時,是否嚴格限制加工。 默認值False。
False:忽略報錯,數據加工繼續處理,返回default定義的值。
True:直接報錯,數據加工不再繼續處理,直接丟棄該條日志。
返回結果
返回轉換后的JSON對象。
函數示例
示例1:提取content字段的JSON值。
原始日志
content: {"abc": 123, "xyz": "test" }
加工規則
e_set("json", json_parse(v("content")))
加工結果
content: {"abc": 123, "xyz": "test" } json: {"abc": 123, "xyz": "test"}
示例2:提取content字段的值,如果不是JSON格式,則返回default的值。
原始日志
content: this is not json
加工規則
e_set("json", json_parse(v("content"), default="FFF", restrict=False))
加工結果
content: this is not json json: FFF
xml_to_json
將xml數據轉成JSON數據。
函數格式
xml_to_json(source)
參數說明
參數
類型
是否必填
說明
source
String
是
傳入需要被轉換的字段。
返回結果
返回轉換后的JSON數據。
函數示例
原始日志
str : <data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data>
加工規則
e_set("str_json",xml_to_json(v("str")))
加工結果
str:<data><country name="Liechtenstein"><rank>1</rank><year>2008</year><gdppc>141100</gdppc><neighbor name="Austria" direction="E"/><neighbor name="Switzerland" direction="W"/></country><country name="Singapore"><rank>4</rank><year>2011</year><gdppc>59900</gdppc><neighbor name="Malaysia" direction="N"/></country><country name="Panama"><rank>68</rank><year>2011</year><gdppc>13600</gdppc><neighbor name="Costa Rica" direction="W"/><neighbor name="Colombia" direction="E"/></country></data> str_json:{"data": {"country": [{"@name": "Liechtenstein", "rank": "1", "year": "2008", "gdppc": "141100", "neighbor": [{"@name": "Austria", "@direction": "E"}, {"@name": "Switzerland", "@direction": "W"}]}, {"@name": "Singapore", "rank": "4", "year": "2011", "gdppc": "59900", "neighbor": {"@name": "Malaysia", "@direction": "N"}}, {"@name": "Panama", "rank": "68", "year": "2011", "gdppc": "13600", "neighbor": [{"@name": "Costa Rica", "@direction": "W"}, {"@name": "Colombia", "@direction": "E"}]}]}}