Darren Christopher
Darren Christopher

Reputation: 4779

Python - JSON array to DataFrame

I have this following JSON array.

[
    {
        "foo"=1
    },
    {
        "foo"=2
    },
    ...
]

I would like to convert it to DataFrame object using pd.read_json() command like below.

df = pd.read_json(my_json) #my_json is JSON array above

However, I got the error, since my_json is a list/array of json. The error is ValueError: Invalid file path or buffer object type: <class 'list'>.

Besides iterating through the list, is there any efficient way to extract/convert the JSON to DataFrame object?

Upvotes: 16

Views: 33616

Answers (2)

Ami Tavory
Ami Tavory

Reputation: 76297

There are two problems in your question:

  1. It called to_csv on a list.
  2. The JSON was illegal, as it contained = signs instead of :

This works by me:

import json
import pandas as pd

>>> pd.DataFrame(json.loads("""[
    {
        "foo": 1
    },
    {
        "foo": 2
    }
]"""))

   foo
0    1
1    2

You can also call read_json directly.

Upvotes: 5

Rakesh
Rakesh

Reputation: 82765

Use df = pd.DataFrame(YourList)

Ex:

import pandas as pd

d = [
    {
        "foo":1
    },
    {
        "foo":2
    }
]

df = pd.DataFrame(d)
print(df)

Output:

   foo
0    1
1    2

Upvotes: 24

Related Questions