dict()

Creates and returns a map (object) from a list of key-value pairs.

Syntax

dict([KEY1, VALUE1, KEY2, VALUE2, ...])

Parameters

KEY1, VALUE1, KEY2, VALUE2, ...
Keys and values specified as pairs. If no arguments are provided, an empty map is returned. Keys must be strings; values can be of any type. The number of arguments must be even.

Description

The dict() function accepts a list of key-value pairs and returns a map. If a key is not a string, that pair is excluded from the map. If the same key is specified more than once, the later value is used. Passing an odd number of arguments causes an error during the parsing phase.

Error codes

Error codeDescription
91060An odd number of arguments was provided.

Usage examples

  1. Creating an empty map

    json "{}" | eval result = dict()
    | # result: {}
    
  2. Creating a map with string key-value pairs

    json "{}" | eval result = dict("name", "alice", "age", 30)
    | # result: {"name":"alice","age":30}
    
  3. Creating a map with values of various types

    json "{}" | eval result = dict("host", ip("192.0.2.1"), "ports", array(80, 443))
    | # result: {"host":"192.0.2.1","ports":[80,443]}
    
  4. NULL input (pair is excluded when the key is null)

    json "{'val': null}" | eval result = dict(val, "skip", "key", "value")
    | # result: {"key":"value"}
    

Compatibility

The dict() function has been available since before Sonar 4.0.