skip to Main Content

I have a JSON file called "values.json" with this content:

{
  "values": [{
    "id": 2,
    "value": "passed"
  }, {
    "id": 41,
    "value": "passed"
  }, {
    "id": 73,
    "value": "passed"
  }, {
    "id": 110,
    "value": "failed"
  }, {
    "id": 122,
    "value": "failed"
  }, {
    "id": 234,
    "value": "passed"
  }, {
    "id": 238,
    "value": "passed"
  }, {
    "id": 345,
    "value": "passed"
  }, {
    "id": 653,
    "value": "passed"
  }, {
    "id": 690,
    "value": "failed"
  }, {
    "id": 5321,
    "value": "passed"
  }, {
    "id": 5322,
    "value": "failed"
  }]
}

I try to convert it to dataframe with next code:

values_path = "values.json"
df = pd.read_json(values_path)
with open(values_path) as f:
    d = json.load(f)
df = pd.json_normalize(d)
print(df)

However, I get the table with one column and one row:

                                              values
0  [{'id': 2, 'value': 'passed'}, {'id': 41, 'val...

My desired output would be something like that:

id value
0 2 passed
1 41 passed
2 73 passed

What do I do wrong? Or maybe there is something wrong with my JSON file? I will be thankful for any help!

3

Answers


  1. The data you want is inside the values key, so you just need to select it:

    df = pd.json_normalize(d['values'])
    
          id   value
    0      2  passed
    1     41  passed
    2     73  passed
    ..   ...     ...
    9    690  failed
    10  5321  passed
    11  5322  failed
    
    [12 rows x 2 columns]
    
    Login or Signup to reply.
  2. with open("values.json") as f:
        d = json.load(fp=f)
    df = pd.DataFrame(data=d.get("values"))
    
    Login or Signup to reply.
  3. import pandas as pd
    import json
    
    with open("values.json", "r") as f:
        data = json.load(f)
    df = pd.json_normalize(data["values"]
    print(df)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search