I have a nested json that I need to explode using posexplode\_outer function

```
def flatten_df(nested_df): 
    for column in nested_df.columns:
        array_cols = [c[0] for c in nested_df.dtypes if c[1][:5] == 'array']
    for column in array_cols:
        nested_df=nested_df.select('*',F.posexplode_outer(nested_df["`"+column+"`"]).alias("position",column))

    nested_cols = [c[0] for c in nested_df.dtypes if c[1][:6] == 'struct']

    if len(nested_cols) == 0:
        return nested_df

    flat_cols = [c[0] for c in nested_df.dtypes if c[1][:6] != 'struct']

    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 flatten_df(flat_df)

```

I get ambiguous column name error

> 
> An error was encountered: "Reference <column\_name> is ambiguous, could
> be: <column\_name>, <column\_name>.;"
> 
> 
> 

Can anyone help me resolve this error. Input to the function is a JSON like below which is stored in a Spark dataframe. The JSON is highly nested.

```
{
        "template": 
            {
                "id": "9",
                "header": [
                    {
                        "id": "header",
                        "value": "Find the Right Marker for the Job"
                    },
                    {
                        "id": "section1-header",
                        "value": "Desk-Style Dry Erase Markers"
                    },
                    {
                        "id": "section2-header",
                        "value": "Pen-Style Dry Erase Markers"
                    },
                    {
                        "id": "section3-header",
                        "value": "Jumbo Washable Markers"
                    }
                ],
                "paragraph": [
                    {
                        "id": "description1",
                        "value": ["Desk-style wipe off easily "] 
                    },
                    {
                        "id": "description2",
                        "value": ["Pen-style "]
                    },
                    {
                        "id": "description3",
                        "value": ["banners."] 
                    },
                    {
                        "id": "description4",
                        "value": ["posters"] 
                    }
                ],
                "image": [
                    {
                        "id": "section1-image",
                        "assetId": "S"
                    },
                    {
                        "id": "section2-image",
                        "assetId": "A" 
                    },
                    {
                        "id": "section3-image",
                        "assetId": "34"
                    },
                    {
                        "id": "section4-image",
                        "assetId": "36"
                    }
                ]
            }
            })

```

The expected output for the above example JSON is

[![enter image description here](https://i.stack.imgur.com/lAgYD.png)](https://i.stack.imgur.com/lAgYD.png)