score:2

Accepted answer

The default decimal precesion is DecimalType(38, 18), I am not sure what you are trying to do but

You can cast the current decimal type as

df1.withColumn("map",udfJsonStrToMapDecimal($"json").cast("map<string, decimal(38,6)>"))

Schema:

root
 |-- json: string (nullable = true)
 |-- map: map (nullable = true)
 |    |-- key: string
 |    |-- value: decimal(38,6) (valueContainsNull = true)

Or you can also define the schema and read as JSON directly as

val schema = StructType(StructField("k", DecimalType(38, 6), nullable = false) :: Nil)

val df1 = Seq(("{\"k\":10.004}")).toDF("json")

val result = df1.withColumn("value", from_json($"json", schema))

Schema:

root
 |-- json: string (nullable = true)
 |-- value: struct (nullable = true)
 |    |-- k: decimal(38,6) (nullable = true)

Related Query

More Query from same tag