Forum Discussion

Richtpt's avatar
Richtpt
Helper I
5 months ago
Solved

How to reference a JSON array element with no name using a Notebook or OPENROWSET?

I'm extremely new to notebooks and accessing data within JSON files that have been imported into a Lakehouse.  I have the following JSON schema in a file (from notebook df.printSchema()): The ...
  • v-nmadadi-msft's avatar
    5 months ago

    Hi Richtpt  ,
    Thanks for reaching out to the Microsoft Fabric Community forum.

    If you just want to see the data in the same nested format as it appears in the JSON, you can run:

    df.select("PRCCollection.Modifiers").show()

    Since both PRCCollection and Modifiers are arrays, Spark will display the result as something like [[US]]. That simply means there is an array inside another array.

     

    If you’d prefer to flatten the structure and extract the actual value, you can use explode() to expand each array level:

    from pyspark.sql.functions import explode
    
    
    
    df2 = df.withColumn("prc", explode("PRCCollection")) \
    
            .withColumn("Modifier", explode("prc.Modifiers"))
    
    
    
    df2.select("Modifier").show()

    Here, the first explode() opens up the PRCCollection array, and the second one expands the Modifiers array inside it. The output will then show the value directly (for example US) instead of the nested [[US]] structure.

     



    For reference this is the output of df.schema() for my data

     

     


    I hope this information helps. Please do let us know if you have any further queries.
    Thank you