valueof()
Returns the value at a specified key or index position from a map or array.
Syntax
valueof(COMPOUND, KEY)
Parameters
COMPOUND- The map or array from which to extract a value.
KEY- A key string for maps, or an integer index for arrays. The value types accepted as an array index differ by series. In the 4.0 series starting with 4.0.2609.0, all numeric types are accepted, and an out-of-range index returns
null. The 5.0 series (through 5.0.2605.2) accepts only the integer type and returnsnullfor any other numeric type.
Description
The valueof() function extracts and returns the value at a specific position from a compound object.
- If
COMPOUNDis a map: returns the value associated withKEY. - If
COMPOUNDis an array: usesKEYas an integer index and returns the element at that position. Indices start at0.
The function returns null in the following situations:
COMPOUNDorKEYisnullCOMPOUNDis neither a map nor an array- A string key is specified for an array
- An integer index is specified for a map (when the map's key type is not integer)
- The index is out of the valid range of the array
Error codes
N/A
Usage examples
-
Extract a value from an array by index (indices start at
0)json "{}" | eval foods = array("Apple", "Banana", "Cucumber") | eval food = valueof(foods, 2) | # food: Cucumber -
Extract a value from a map by key
json "{}" | eval foods = dict("a", "Apple", "b", "Banana", "c", "Cucumber") | eval food = valueof(foods, "b") | # food: Banana -
COMPOUNDisnulljson "{}" | eval food = valueof(null, "b") | # food: null -
Array index is out of the valid range
json "{}" | eval foods = array("Apple", "Banana") | eval food = valueof(foods, 5) | # food: null
Change history
valueof() has been available since before Sonar 4.0.