<?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 flatten non standard Json files in a dataframe in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23489#M16214</link>
    <description>&lt;P&gt;@Hubert Dudek​&amp;nbsp;, &amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your answer. That helped. I still w,&lt;/P&gt;&lt;P&gt;onder how i can keep the connected data between the Data level and the "lines" level. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# i would like to keep reference to invoiceno in lines.*
dfl = invoices.select("lines.*","invoiceno")
&amp;nbsp;
lines = spark.createDataFrame(dfl.rdd.flatMap(lambda x: [y for y in x]))
&amp;nbsp;
# returns error message below
display(lines)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I enclose the notebook/DBC file with comments questions as well. &lt;/P&gt;</description>
    <pubDate>Thu, 07 Apr 2022 18:18:34 GMT</pubDate>
    <dc:creator>steelman</dc:creator>
    <dc:date>2022-04-07T18:18:34Z</dc:date>
    <item>
      <title>how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23486#M16211</link>
      <description>&lt;P&gt;hello, &lt;/P&gt;&lt;P&gt;I have a non standard Json file with a nested file structure that I have issues with.  Here is an example of the json file. &lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;jsonfile= """[ {
      "success":true,
      "numRows":2,
      "data":{
         "58251":{
            "invoiceno":"58251",
            "name":"invoice1",
            "companyId":"1000",
            "departmentId":"1",
            "lines":{
               "40000":{
                  "invoiceline":"40000",
                  "productid":"1",
                  "amount":"10000",
                  "quantity":"7"
               },
               "40001":{
                  "invoiceline":"40001",
                  "productid":"2",
                  "amount":"9000",
                  "quantity":"7"
               }
            }
         },
         "58252":{
            "invoiceno":"58252",
            "name":"invoice34",
            "companyId":"1001",
            "departmentId":"2",
            "lines":{
               "40002":{
                  "invoiceline":"40002",
                  "productid":"3",
                  "amount":"7000",
                  "quantity":"6"
               },
               "40003":{
                  "invoiceline":"40003",
                  "productid":"2",
                  "amount":"9000",
                  "quantity":"7"
               }            }         }      }   }]"""
import pandas as pd
df = pd.read_json(jsonfile)
display(df)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The problem here is that some of the keys are used as "58252", instead of "id":"58252" or blank value. This is the case for both the "Data" array and the "lines" array. So I have tried using standard functions in spark with json_normalize or explode but it doesnt seem to work with this particular json format. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The endgoal is to get a dataframe looking like this:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="desired format in the dataframe after processing the json file"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1964i7A7436E09A6D14B5/image-size/large?v=v2&amp;amp;px=999" role="button" title="desired format in the dataframe after processing the json file" alt="desired format in the dataframe after processing the json file" /&gt;&lt;/span&gt;I hope you can give me some advice on how to approach and handle these files in databricks.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 08:11:36 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23486#M16211</guid>
      <dc:creator>steelman</dc:creator>
      <dc:date>2022-04-07T08:11:36Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23487#M16212</link>
      <description>&lt;P&gt;I am including example code, as well notebook source and screenshot.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;rddjson = sc.parallelize([jsonfile])
df = (spark.read
.json(rddjson)
.select("data.*"))
invoices = spark.createDataFrame(df.rdd.flatMap(lambda x: [y for y in x]))&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image.png"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1959i2C77D3CE4838B414/image-size/large?v=v2&amp;amp;px=999" role="button" title="image.png" alt="image.png" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 11:17:49 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23487#M16212</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2022-04-07T11:17:49Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23488#M16213</link>
      <description>&lt;P&gt;so in the next step you can do something similar for lines&lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 11:19:10 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23488#M16213</guid>
      <dc:creator>Hubert-Dudek</dc:creator>
      <dc:date>2022-04-07T11:19:10Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23489#M16214</link>
      <description>&lt;P&gt;@Hubert Dudek​&amp;nbsp;, &amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks a lot for your answer. That helped. I still w,&lt;/P&gt;&lt;P&gt;onder how i can keep the connected data between the Data level and the "lines" level. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;# i would like to keep reference to invoiceno in lines.*
dfl = invoices.select("lines.*","invoiceno")
&amp;nbsp;
lines = spark.createDataFrame(dfl.rdd.flatMap(lambda x: [y for y in x]))
&amp;nbsp;
# returns error message below
display(lines)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;I enclose the notebook/DBC file with comments questions as well. &lt;/P&gt;</description>
      <pubDate>Thu, 07 Apr 2022 18:18:34 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23489#M16214</guid>
      <dc:creator>steelman</dc:creator>
      <dc:date>2022-04-07T18:18:34Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23490#M16215</link>
      <description>&lt;P&gt;@stale stokkereit​&amp;nbsp;&lt;/P&gt;&lt;P&gt;You can use the below function to flatten the struct field&lt;/P&gt;&lt;PRE&gt;&lt;CODE&gt;import pyspark.sql.functions as F
&amp;nbsp;
def flatten_df(nested_df):
    flat_cols = [c[0] for c in nested_df.dtypes if c[1][:6] != 'struct']
    nested_cols = [c[0] for c in nested_df.dtypes if c[1][:6] == 'struct']
&amp;nbsp;
    flat_df = nested_df.select(flat_cols +
                               [F.col(nc+'.'+c).alias(nc+'_'+c)
                                for nc in nested_cols
                                for c in nested_df.select(nc+'.*').columns])
    return flat_df&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Your dataframe would look like this:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper" image-alt="image"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/1975i88D8D252C863BADF/image-size/large?v=v2&amp;amp;px=999" role="button" title="image" alt="image" /&gt;&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 13 May 2022 16:37:50 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23490#M16215</guid>
      <dc:creator>Deepak_Bhutada</dc:creator>
      <dc:date>2022-05-13T16:37:50Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23491#M16216</link>
      <description>&lt;P&gt;Hi @stale stokkereit​,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Just a friendly follow-up. Do you still need help or did any of the responses helped you? please let us know&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 18:22:11 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23491#M16216</guid>
      <dc:creator>jose_gonzalez</dc:creator>
      <dc:date>2022-06-07T18:22:11Z</dc:date>
    </item>
    <item>
      <title>Re: how to flatten non standard Json files in a dataframe</title>
      <link>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23492#M16217</link>
      <description>&lt;P&gt;Thanks for following up. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I didn'get any answers from this forum which I could use directly, but it did help me to move forward. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;But I still figured out a solution to the problem after more study of the subject.&lt;/P&gt;&lt;P&gt;The best solution was to write a schema  before importing the json file, It tooks some time to write the schema and get in correct format, Ex &lt;/P&gt;&lt;P&gt;StructField("data",&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;MapType(StringType(),StructType([&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;StructField("invoiceId", LongType(),True),&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After using a schema, spark was able to understand that the numbering sequence on the node below data, was a struct type, that was handled correctly. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;After the schema was correctly defined for the json file,  it was possible to use spark to do explode operations on the struct node "data"&lt;/P&gt;&lt;P&gt;Ex:&lt;/P&gt;&lt;P&gt;from pyspark.sql.functions import explode, col&lt;/P&gt;&lt;P&gt;df1 = dfresult.select(explode('data'))&lt;/P&gt;&lt;P&gt;df2 = df1.select("value.*")&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;In the end I got all data into a normalized table. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;thanks for all contributions and efforts to help. &lt;/P&gt;</description>
      <pubDate>Tue, 07 Jun 2022 20:49:42 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-to-flatten-non-standard-json-files-in-a-dataframe/m-p/23492#M16217</guid>
      <dc:creator>steelman</dc:creator>
      <dc:date>2022-06-07T20:49:42Z</dc:date>
    </item>
  </channel>
</rss>

