len()

The len() function returns the length or element count of a string, array, list, map, or binary value.

Syntax

len(VALUE)

Parameters

VALUE
The value whose length to measure. Supports string, array, list, map, and binary (byte[]) types.

Description

The len() function behaves as follows depending on the type of VALUE:

  • String: Returns the number of characters.
  • Array or list: Returns the number of elements.
  • Map (object): Returns the number of key-value pairs.
  • Binary (byte[]): Returns the number of bytes.
  • Other types: Converts the value using toString() and returns the number of characters.

If VALUE is null, the function returns 0. The return type is a 32-bit integer.

Error codes

N/A

Usage examples

To prepare the WEB_APACHE_SAMPLE table used in these examples, refer to Preparing sample data.

  1. Return the length of a URI string

    table limit=5 WEB_APACHE_SAMPLE | eval result = len(uri) | fields uri, result
    
  2. Return the length of a User-Agent string

    table limit=5 WEB_APACHE_SAMPLE | eval result = len(agent) | fields agent, result
    
  3. NULL input

    json "{'val': null}" | eval result = len(val)
    | # result: 0
    
  4. Return the number of elements in an array

    json "{'val': [1, 2, 3]}" | eval result = len(val)
    | # result: 3
    

Compatibility

The len() function is available since before Sonar 4.0.