cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
cancel
Showing results for 
Search instead for 
Did you mean: 

Importing irregularly formatted json files

MikeJohnsonZa
New Contributor

Hi

I'm importing a large collection of json files, the problem is that they are not what I would expect a well-formatted json file to be (although probably still valid), each file consists of only a single record that looks something like this (this is just an abstraction)

[{"name":"myName","Surname":"MySurname"},[{"address":"1","Type":"Home"},{"address":"2","Type":"Home"}],[{"Tel":"1"},{"Tel":"2"}]]

I would ideally like to import it using the standard json read option bus can't figure out how to structure the schema.

My first approach involved creating a UDF that imported the record as a string and returned a new properly formatted object

def structure(object):

  structure = {}

  structure["name"] = object[0]["name"]

  structure["Surname"] = object[0]["Surname"]

  structure["addresses"] = []

  structure["telephones"] = []

  for address in object[1]:

    structure["addresses"].append({"address": address["address"],"Type": address["Type"]})

  for telephones in object[2]:

    structure["telephones"].append({"Tel": telephones["Tel"]})

  return structure

This works but it will be slower and less intuitive.

using a schema like this "works" but the elements in the ID'd come back as null

mySchema = StructType([

          StructField("Name", StringType(), True),

          StructField("Surname", StringType(), True),

          StrutType(StructField("IDs",

            StructType([StructField("ID", StringType(), True)])

          )

             

      )

   ])

1 ACCEPTED SOLUTION

Accepted Solutions

jose_gonzalez
Moderator
Moderator

Hi @Michael Johnson​,

I would like to share the following notebook which contains examples on how to process complex data types, like JSON. Please check the following link and let us know if you still need help https://docs.databricks.com/optimizations/complex-types.html

View solution in original post

3 REPLIES 3

Debayan
Esteemed Contributor III
Esteemed Contributor III

Hi, when you are mentioning irregularly formatted json files, does the source look like this? or when it is pasted it looks like json format is bad. Please explain it a little more.

Anonymous
Not applicable

Hi @Michael Johnson​ 

Hope all is well! Just wanted to check in if you were able to resolve your issue and would you be happy to share the solution or mark an answer as best? Else please let us know if you need more help. 

We'd love to hear from you.

Thanks!

jose_gonzalez
Moderator
Moderator

Hi @Michael Johnson​,

I would like to share the following notebook which contains examples on how to process complex data types, like JSON. Please check the following link and let us know if you still need help https://docs.databricks.com/optimizations/complex-types.html

Welcome to Databricks Community: Lets learn, network and celebrate together

Join our fast-growing data practitioner and expert community of 80K+ members, ready to discover, help and collaborate together while making meaningful connections. 

Click here to register and join today! 

Engage in exciting technical discussions, join a group with your peers and meet our Featured Members.