How to relationalize JSON containing arrays

假如想象 提交于 2021-01-28 14:09:14

问题


I am using AWS Glue to read data file containing JSON (on S3). This one is a JSON with data contained in array. I have tried using relationalize() function but it doesn't work on array. It does work on nested JSON but this is not the data format of input.

Is there a way to relationalize JSON with arrays in it?

Input data:

{
    "ID":"1234",
    "territory":"US",
    "imgList":[
        {
            "type":"box"
            "locale":"en-US"
            "url":"boxart/url.jpg"
        },
        {
            "type":"square"
            "locale":"en-US"
            "url":"square/url.jpg"
        }
    ]
}

Code:

dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
dfc.select('root').toDF().show()

Output:

+----+----------+--------+
|ID  |territory |imgList |
+----+----------+--------+
|1234|       US |       1|
+----+----------+--------+

Desired output:

+----+----------+-------------+---------------+---------------+
|ID  |territory |imgList.type |imgList.locale |imgList.url    |
+----+----------+-------------+---------------+---------------+
|1234|       US |       box   |         en-US |boxart/url.jpg |
+----+----------+-------------+---------------+---------------+
|1234|       US |       square|         en-US |square/url.jpg |
+----+----------+-------------+---------------+---------------+

回答1:


Relationalize creates DynamicFrames for each arrays in the JSON document. So you just need to get it and join with the root table:

dfc = Relationalize.apply(frame = datasource0, staging_path = glue_temp_storage, name = "root", transformation_ctx = "dfc")
root_df = dfc.select('root')
imgList_df = dfc.select('root_imgList')

df = Join.apply(root_df, imgList_df, 'imgList', 'id')
df.toDF().show()


来源:https://stackoverflow.com/questions/55251576/how-to-relationalize-json-containing-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!