[Python] which is the best way to convert json into a dataframe?

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I have a question about the best way to convert this JSON to a Dataframe:

    Json data:

    {
    "myschema": {
    "accounts": {
    "load_type": "daily",
    "fields": {
    "id": "nvarchar2",
    "isdeleted": "number",
    "master": "nvarchar2",
    "name": "nvarchar2"
    }
    },
    "customer": {
    "load_type": "daily",
    "fields": {
    "id": "nvarchar2",
    "accountid": "nvarchar2",
    "usergroupid": "nvarchar2"
    }
    },
    "resources": {
    "load_type": "daily",
    "fields": {
    "id": "nvarchar2",
    "isdeleted": "number",
    "name": "nvarchar2",
    "currency": "nvarchar2"
    }
    },
    ....
    ....
    }
    }


    The result must be something like this:

    TABLE |LOAD_TYPE |COLUMN |COLUMN_TYPE |
    +-----------+-----------+-----------------+--------------+
    | accounts |daily |id |NVARCHAR2 |
    | accounts |daily |master |NVARCHAR2 |
    | accounts |daily |name |NVARCHAR2 |
    | customer |daily |id |NVARCHAR2 |
    | customer |daily |accountid |NVARCHAR2 |
    | customer |daily |usergroupid |NVARCHAR2 |
    | resources |daily |id |NVARCHAR2 |
    | resources |daily |name |NVARCHAR2 |
    | resources |daily |currency |NVARCHAR2 |
    +-----------+-----------+-----------------+--------------+


    I tried the next code:

    df2 = spark.read.option("multiLine", "true").json(json_s3_path)
    df2.printSchema()

    root
    |-- mySchema: struct (nullable = true)
    | |-- accounts: struct (nullable = true)
    | | |-- FIELDS: struct (nullable = true)
    .....
    .....


    and also the next code:

    df3 = spark.read.format("json") \
    .option("multiLine", True) \
    .option("header",True) \
    .option("inferschema",True) \
    .load(json_s3_path) \


    and the result for this is:

    +----------------------------------------------------------------------------------------------------------------------------------------------------+
    |mySchema |
    +----------------------------------------------------------------------------------------------------------------------------------------------------+
    |{{{NVARCHAR2, NUMBER, NVARCHAR2, NVARCHAR2}, Delta}, {{NVARCHAR2, NVARCHAR2, NVARCHAR2}, Delta}, {{NVARCHAR2, NVARCHAR2, NUMBER, NVARCHAR2}, Delta}}|
    +----------------------------------------------------------------------------------------------------------------------------------------------------+

    Continue reading...

Compartilhe esta Página