Structured Data Functions
This section describes the syntax rules of structured data functions, including parameter description and function examples.
Type |
Function |
Description |
---|---|---|
JSON |
Extracts or calculates specific values in JSON expressions based on JMES syntax. |
|
Parses a value into a JSON object. |
||
XML |
Converts XML data to JSON data. |
json_select
This function extracts or calculates specific values in JSON expressions based on JMES syntax.
- Function format
json_select(value, jmes, default=None, restrict=false)
- Description
Parameter
Type
Mandatory
Description
value
String and JSON
Yes
JSON expression or field of the field to be extracted.
jmes
String
Yes
JMES expression, which indicates the extracted field.
default
String
No
The value returned if the field to be extracted does not exist. The default value is None, indicating that no field is returned.
restrict
Bool
No
Whether to strictly restrict the processing when the value of the extracted field is not in the valid JSON format.
- false (default value): Ignores the error, continues to process the data, and returns the value defined by default.
- true: Reports an error, discontinues data processing, and discards the log.
- Returned result
The extracted value.
- Function example
- Example 1: Extract the value of name from content.
- Test data
{ "content": {"name": "xiaoming", "age": 10} }
- Processing rule
e_set("json_filter",json_select(v("content"), "name"))
- Processing result
content: {"name": "xiaoming", "age": 10} json_filter: xiaoming
- Test data
- Example 2: Extract all values of name from content.
- Test data
{ "content": {"name": ["xiaoming", "xiaowang", "xiaoli"], "age": 10} }
- Processing rule
e_set("json_filter", json_select(v("content"), "name[*]"))
- Processing result
content: {"name": ["xiaoming", "xiaowang", "xiaoli"], "age": 10} json_filter: ["xiaoming", "xiaowang", "xiaoli"]
- Test data
- Example 3: Extract the value of name3 from content. If the field does not exist, the value of default is returned.
- Test data
{ "content": {"name": "xiaoming", "age": 10} }
- Processing rule
e_set("json_filter", json_select(v("content"), "name3", default="None"))
- Processing result
content: {"name": "xiaoming", "age": 10} json_filter: None
- Test data
- Example 4: Extract the value of name-test that contains hyphens (-) from content.
- Test data
{ "content": {"name": {"name-test":"xiaoming"}, "age": 10} }
- Processing rule
e_set("json_filter", json_select(v("content"), 'name."name-test"', default=None))
- Processing result
content: {"name": {"name-test":"xiaoming"}, "age": 10} json_filter: xiaoming
- Test data
- Example 5: Extract the value of name-test that contains hyphens (-) from content. If the element does not exist, no field is returned.
- Test data
{ "content": {"name": {"name.test":"xiaoming"}, "age": 10} }
- Processing rule
e_set("json_filter", json_select(v("content"), 'name."name-test"', default=None))
- Processing result
content: {"name": {"name-test":"xiaoming"}, "age": 10}
- Test data
- Example 1: Extract the value of name from content.
json_parse
This function parses the value into a JSON object.
- Function format
json_parse(value, default=None, restrict=false)
- Description
Parameter
Type
Mandatory
Description
value
String
Yes
Field to be parsed.
default
String
No
The value returned if the field to be parsed does not exist. The default value is None, indicating that no field is returned.
restrict
Bool
No
Whether to strictly restrict processing when the value of the field to be parsed is not in the valid JSON format.
- false (default value): Ignores the error, continues to process the data, and returns the value defined by default.
- true: Reports an error, discontinues data processing, and discards the log.
- Returned result
The converted JSON object.
- Function example
- Example 1: Extract the JSON value of content.
- Test data
{ "content": {"abc": 123, "xyz": "test" } }
- Processing rule
e_set("json", json_parse(v("content")))
- Processing result
content: {"abc": 123, "xyz": "test" } json: {"abc": 123, "xyz": "test"}
- Test data
- Example 2: Extract the value of content. If the value is not in JSON format, the value of default is returned.
- Test data
{ "content": "this is not json" }
- Processing rule
e_set("json", json_parse(v("content"), default="FFF", restrict=false))
- Processing result
content: this is not json json: FFF
- Test data
- Example 1: Extract the JSON value of content.
xml_to_json
This function converts XML data to JSON data.
- Function format
xml_to_json(source)
- Description
Parameter
Type
Mandatory
Description
source
String
Yes
Field to be converted.
- Returned result
The converted JSON data.
- Function example
- Test data
{ "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>" }
- Processing rule
e_set("str_json",xml_to_json(v("str")))
- Processing result
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"}]}]}}
- Test data
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot