0

I have a dataframe structure as shown below:

+----+----+--------------------+
|col1|col2|col3                |
+----+----+--------------------+
|1   |2   |{"key1" : "val1"}   |
|3   |4   |{"key2" : "val2"}   |
+----+----+--------------------+

col3 is a Json String. I would like to add another column to the dataframe that will convert col3 to a MAP. Something like this:

+----+----+--------------------+----------------+
|col1|col2|col3                |col4            |
+----+----+--------------------+----------------+
|1   |2   |{"key1" : "val1"}   |[key1 -> val1]  |
|3   |4   |{"key2" : "val2"}   |[key2 -> val2]  |
+----+----+--------------------+----------------+

My Scala code snippet is something like this:

val df: DataFrame = getRowDataset(payload, schema, "payload") // df is represented by the first column
df.withColumn("col4", "<>") // <What do I need to do here?>

How am I supposed to implement this with Scala?

3

1 Answer 1

0

Keeping link as a reference, the solution is as follows:

    val mappingSchema = MapType(StringType, StringType)
    val df2 = df.withColumn("col4", from_json((df("col3")), mappingSchema))

Note: df("col3") is a Json String in my use case.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.