Spark SQL - Convert Delimited String to Map using str_to_map Function

Kontext Kontext 0 1388 1.24 index 6/4/2022

Spark SQL function str_to_map can be used to split the delimited string to key value pairs.

Function syntax

 str_to_map(text[, pairDelim[, keyValueDelim]]) 

The default values for the parameters are:

  • pairDelim: ,
  • keyValueDelim: :

Code snippets

The following code snippets convert string to map object:

spark-sql> select str_to_map('1:a,2:b,3:c,4:d');
str_to_map(1:a,2:b,3:c,4:d, ,, :)
{"1":"a","2":"b","3":"c","4":"d"}
spark-sql-function

Join the Discussion

View or add your thoughts below

Comments