<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How to convert a string column to Array of Struct ? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18134#M11989</link>
    <description>&lt;P&gt;I've marked my comment as best . Does anything else need to be done ? &lt;/P&gt;</description>
    <pubDate>Thu, 09 Jun 2022 09:43:53 GMT</pubDate>
    <dc:creator>Gopal_Sir</dc:creator>
    <dc:date>2022-06-09T09:43:53Z</dc:date>
    <item>
      <title>How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18130#M11985</link>
      <description>&lt;P&gt;I have a nested struct , where on of the field is a &lt;B&gt;string &lt;/B&gt;, it looks something like this ....&lt;/P&gt;&lt;P&gt;string = &lt;/P&gt;&lt;P&gt;"[{\"to_loc\":\"6183\",\"to_loc_type\":\"S\",\"qty_allocated\":\"18\"},{\"to_loc\":\"6137\",\"to_loc_type\":\"S\",\"qty_allocated\":\"9\"},{\"to_loc\":\"6088\",\"to_loc_type\":\"S\",\"qty_allocated\":\"9\"}]"&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;my goal is to get it into a &lt;B&gt;Array &lt;/B&gt;of &lt;B&gt;Struct , &lt;/B&gt;so that each struct in this string can be exploded into a new row . Like this , &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;C1,C2, C3&lt;/P&gt;&lt;P&gt;_   , _ , {"to_loc\":\"6183\",\"to_loc_type\":\"S\",\"qty_allocated\":\"18\}&lt;/P&gt;&lt;P&gt;_   , _ , {"to_loc\":\"6137\",\"to_loc_type\":\"S\",\"qty_allocated\":\"9\}&lt;/P&gt;&lt;P&gt;_   , _ , {"to_loc\":\"6088\",\"to_loc_type\":\"S\",\"qty_allocated\":\"9\}&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;so that finally each of those keys can also be taken out as a new column &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I've tried by casting the string column into  array of struct , but spark is refusing to convert my string column . Any help on this &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the final schema = &lt;/P&gt;&lt;P&gt;ArrayType(StructType(&lt;/P&gt;&lt;P&gt;[StructField("to_loc",StringType(),True),&lt;/P&gt;&lt;P&gt;StructField("to_loc_type",StringType(),True),&lt;/P&gt;&lt;P&gt;StructField("qty_allocated",StringType(),True)]&lt;/P&gt;&lt;P&gt;))&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 06:47:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18130#M11985</guid>
      <dc:creator>Gopal_Sir</dc:creator>
      <dc:date>2022-06-09T06:47:42Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18131#M11986</link>
      <description>&lt;P&gt;Ok this is not a complete answer, but my first guess would be to use the explode() or posexplode() function to create separate records of the array members.&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 07:31:39 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18131#M11986</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-06-09T07:31:39Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18132#M11987</link>
      <description>&lt;P&gt;Ok , so I got it working . &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Call the from_json() function with string column as input and the schema at second parameter . It will convert it into struct . &lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 08:00:12 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18132#M11987</guid>
      <dc:creator>Gopal_Sir</dc:creator>
      <dc:date>2022-06-09T08:00:12Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18133#M11988</link>
      <description>&lt;P&gt;Can you mark the question as answered so others can find the solution?&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 08:04:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18133#M11988</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-06-09T08:04:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18134#M11989</link>
      <description>&lt;P&gt;I've marked my comment as best . Does anything else need to be done ? &lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 09:43:53 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18134#M11989</guid>
      <dc:creator>Gopal_Sir</dc:creator>
      <dc:date>2022-06-09T09:43:53Z</dc:date>
    </item>
    <item>
      <title>Re: How to convert a string column to Array of Struct ?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18135#M11990</link>
      <description>&lt;P&gt;Nope &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 09 Jun 2022 10:11:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-convert-a-string-column-to-array-of-struct/m-p/18135#M11990</guid>
      <dc:creator>-werners-</dc:creator>
      <dc:date>2022-06-09T10:11:17Z</dc:date>
    </item>
  </channel>
</rss>

