Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing JSON into Pandas

I have to following JSON that is coming from an API (e.g. my_json). The array of entities is stored in a key called entities:

{
    "action" : "get",
    "application" : "4d97323f-ac0f-11e6-b1d4-0eec2415f3df",
    "params" : {
      "limit" : [ "2" ]
    },
    "path" : "/businesses",
    "entities" : [
        {
            "uuid" : "508d56f1-636b-11e7-9928-122e0737977d",
            "type" : "business",
            "size" : 730 },
        {
            "uuid" : "2f3bd4dc-636b-11e7-b937-0ad881f403bf",
            "type" : "business",
            "size" : 730 
        } ],
  "timestamp" : 1499469891059,
  "duration" : 244,
  "count" : 2
}

I am trying to load them into a data frame as follows:

import pandas as pd

pd.read_json(my_json['entities'], orient='split')

I get the following error:

ValueError: Invalid file path or buffer object type: <type 'list'>

I have tried records orientation and still doesn't work.

like image 602
MoreScratch Avatar asked Jul 07 '17 23:07

MoreScratch


People also ask

Can pandas import JSON?

Pandas allow you to convert a list of lists into a Dataframe and specify the column names separately. A JSON parser transforms a JSON text into another representation must accept all texts that conform to the JSON grammar. It may accept non-JSON forms or extensions.

How read JSON data in pandas?

Reading JSON Files using Pandas To read the files, we use read_json() function and through it, we pass the path to the JSON file we want to read. Once we do that, it returns a “DataFrame”( A table of rows and columns) that stores data.

How do I import a JSON into a DataFrame in Python?

Pandas Convert JSON String to DataFrame You can load JSON string using json. loads() function. Pass JSON object to json_normalize() , which returns a Pandas DataFrame.


2 Answers

If my_json is a dictionary as I suspect, then you can skip the pd.read_json and just do

pd.DataFrame(my_json['entities'])

   size      type                                  uuid
0   730  business  508d56f1-636b-11e7-9928-122e0737977d
1   730  business  2f3bd4dc-636b-11e7-b937-0ad881f403bf
like image 165
piRSquared Avatar answered Sep 21 '22 19:09

piRSquared


According to the official documenation orient is suppose to be 'records'

df = pd.read_json(json.dumps(b_j['entities']) , orient='records')
like image 42
Avi Gaur Avatar answered Sep 25 '22 19:09

Avi Gaur