Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do I read JSON using pandas?

How do I read the json below in pandas:

[
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "926.00000",
            "os": "And",
            "hr": "0",
            "_id": "34358",
            "month": "10",
            "date": "2021-10-11"
        }
    },
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "830.00000",
            "os": "And",
            "hr": "0",
            "_id": "24158",
            "month": "10",
            "date": "2021-01-02"
        }
    },
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "128.00000",
            "os": "And",
            "hr": "0",
            "_id": "23358",
            "month": "10",
            "date": "2021-10-03"
        }
    }

Current Output

DataVal
{‘sales’: ‘0.00000’, ‘cost’: ‘96.00000’, ‘os’: ‘And’, ‘hr’: ‘0’, ‘_id’: ‘24358’, ‘month’: ’10’, ‘date’: ‘2021-10-01’}
{‘sales’: ‘0.00000’, ‘cost’: ‘96.00000’, ‘os’: ‘And’, ‘hr’: ‘0’, ‘_id’: ‘24358’, ‘month’: ’10’, ‘date’: ‘2021-10-01’}

Expected Output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Sales Cost os
0.000 234 And

Above table with other additional columns like os,hr, _id, month

>Solution :

Read JSON file into data using json

import json 

with open('path/to/json.json') as f:
    data = json.load(f)

Or if it’s from an API call (using requests)

data = response.json()

Then you just need

df = pd.DataFrame(rec['DataVal'] for rec in data)

Output:

>>> df 

     sales       cost   os hr    _id month        date
0  0.00000  926.00000  And  0  34358    10  2021-10-11
1  0.00000  830.00000  And  0  24158    10  2021-01-02
2  0.00000  128.00000  And  0  23358    10  2021-10-03

Setup:

data = [
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "926.00000",
            "os": "And",
            "hr": "0",
            "_id": "34358",
            "month": "10",
            "date": "2021-10-11"
        }
    },
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "830.00000",
            "os": "And",
            "hr": "0",
            "_id": "24158",
            "month": "10",
            "date": "2021-01-02"
        }
    },
    {
        "DataVal": {
            "sales": "0.00000",
            "cost": "128.00000",
            "os": "And",
            "hr": "0",
            "_id": "23358",
            "month": "10",
            "date": "2021-10-03"
        }
    }
]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading