By Forrest Lybarger | Splunk Consultant
JSON data is a very common format in Splunk and users like to have control of the data. Splunk’s collection of json_* commands help users format JSON data at search time so that it can be presented and used without any permanent changes to the indexed data. This guide will help users learn how to use these commands themselves, so they can have full control of their JSON data. It is important to note that the commands are often looking for the name of a field containing a whole JSON event, so if the whole event is one JSON event then _raw can be used to reference the whole event as one field. First is the json_valid command. Using this command, strings can be validated as using proper JSON. It has simple syntax where you input the field containing the JSON data and it returns true or false. If you want to store the value in a field, you could use an if statement.-
- Example:
- | eval valid = if(json_valid(_raw), 1, 0)
- Result:
- valid=1
-
- Example:
- | eval array=json_array(field_name)
- Result:
- array=[“string1”, “string2″, “string3”]
-
- Example:
- | eval obj=json_object(“object1”, json_object(“object2”, json_array(“item1”, “item2”))
- Result:
- Obj={“object1”:{“object2”:[“item1″,”item2”]}}
-
- Example:
- | eval ext=json_extract(_raw, “array{0}”)
- Event:
- {
- “array”: [
- {
- “name”: “item1”,
- “subarray1”: [
- { “name”: “subitem1” },
- { “name”: “subitem2” }
- ]
- },
- {
- “name”: “item2”,
- “subarray2”: [
- { “name”: “subitem1” },
- { “name”: “subitem2” },
- { “name”: “subitem3” }
- ]
- },
- {
- “name”: “item3”,
- “subarray3”: [
- { “name”: “subitem1” },
- { “name”: “subitem2” }
- ]
- }
- ]
- }
- Result:
- ext={“name”: “item1″,”subarray1”: [{ “name”: “subitem1” },{ “name”: “subitem2” }]}
