0

I have a table with two columns id, json_string and need to convert json_string into a MongoDB document format. I'm sending data from Spark/Scala to MongoDB.

I tried using withColumn but I still don't get the desired format. This is what I have so far, so any help would be really appreciated.

Original json string sample (df)

val df=spark.sql("select id, json_string from mytable")

{"id":"0001","json_string":"{\"header\": {\"column1\":\"value1\",\"column2\":\"value2\"},\"tail\": [{\"column3\":\"value3\",\"column4\":\"value4\",\"column5\":\"value5\"}]}"}

Using withColumn (df2) I get this:

val df2=df.withColumn("json_string",from_json(col("json_string"),MapType(StringType,StringType)))

{"id":"0001","json_string":{"header":"{\"column1\":\"value1\",\"column2\":\"value2\"}","tail":"[{\"column3\":\"value3\",\"column4\":\"value4\",\"column5\":\"value5\"}]"}}

Desired format:

{"id":{"$id":"0001"},"header":{"column1":"value1","column2":"value2"},"tail":[{"column3":"value3","column4":"value4","column5":"value5"}]}

Desired format picture sample

1 Answer 1

0

Instead of defining the schema manually, you can get it dynamically and use it with from_json

val json_schema = spark.read.json(df.select("json_string").as[String]).schema
val df2 = df.withColumn("json_string", from_json(col("json_string"), json_schema))
  .select("id", "json_string.*")

Result:

+----+----------------+--------------------------+
|id  |header          |tail                      |
+----+----------------+--------------------------+
|0001|{value1, value2}|[{value3, value4, value5}]|
+----+----------------+--------------------------+

Schema:

root
 |-- id: string (nullable = true)
 |-- header: struct (nullable = true)
 |    |-- column1: string (nullable = true)
 |    |-- column2: string (nullable = true)
 |-- tail: array (nullable = true)
 |    |-- element: struct (containsNull = true)
 |    |    |-- column3: string (nullable = true)
 |    |    |-- column4: string (nullable = true)
 |    |    |-- column5: string (nullable = true)
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome! It worked! I was struggling with this, trying many approaches without success. Thank you, greatly appreciate it.

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.